2022-02-22 swipe questions and punch in every day
All in one -- dynamic programming
1268: [example 9.12] complete knapsack problem
[Title Description]
There are n kinds of articles, each of which has a weight and a value. However, the number of each item is unlimited. At the same time, there is a backpack with a maximum carrying capacity of M. now select several items from n items (the same item can be selected multiple times), so that the sum of its weight is less than or equal to m, and the sum of its value is the maximum.
[input]
The first line: two integers, mm (backpack capacity, M ≤ 200) and NN (item quantity, N ≤ 30N ≤ 30);
Line 2... N+1: two integers Wi,Ci in each line, representing the weight and value of each item.
[output]
Only one line and one number represent the maximum total value.
[input example]
10 4 2 1 3 3 4 5 7 9 [Output example] max=12
Here, because the number of items is small, you can use the writing method of 01 backpack. Each goods can't be loaded in the backpack all the time. The others are the same as 01 backpack.
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<string.h> #include<string> #include<unordered_map> #include<math.h> #include<queue> typedef long long ll; const int N = 210; ll f[N], v[N], w[N]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) cin >> v[i] >> w[i]; for (int i = 0; i < m; i++) { for (int j = n; j >= 0; j--) { for (int k = 1; k * v[i] <= j; k++) { f[j] = max(f[j], f[j - k * v[i]] + k * w[i]); } } } cout << "max=" << f[n] << endl; return 0; }
If the value is too large, it is necessary to change a method. During 01 knapsack operation, the enumeration knapsack space is from large to small, and the complete knapsack is from small to large. In this way, when calculating and enumerating the larger backpacks behind, the results can be calculated faster with the help of the previously calculated small backpacks
#include<iostream> using namespace std; const int N=1010; int f[N],w[N],v[N]; int n,m; int main() { cin>>m>>n; for(int i=0;i<n;i++)cin>>v[i]>>w[i]; for(int i=0;i<n;i++) for(int j=v[i];j<=m;j++) f[j]=max(f[j],f[j-v[i]]+w[i]); cout<<"max="<<f[m]<<endl; return 0; }
1270: [example 9.14] hybrid Backpack
[Title Description]
A traveler has a backpack that can hold up to V kilograms. Now there are n items. Their weights are W1, W2,..., Wn respectively, and their values are C1,C2,..., Cn respectively. Some items can be taken only once (01 backpack), some items can be taken unlimited times (complete backpack), and some items can be taken with an upper limit (multiple backpacks). Solve which items are loaded into the backpack, so that the total cost of these items does not exceed the backpack capacity, and the total value is the largest.
[input]
The first line: two integers, m (backpack capacity, m < = 200), n (number of items, n < = 30);
Line 2... N+1: there are three integers wi, CI and PI in each line. The first two integers respectively represent the weight and value of each article. If the third integer is 0, it means that this article can purchase countless pieces. If it is other numbers, it means the maximum number of pieces (Pi) that can be purchased for this article.
[output]
Only one line and one number represent the maximum total value.
[input example]
10 3 2 1 0 3 3 1 4 5 4
[output example]
11
[tips]
Select 1 item for the first item and 2 items for the third item.
It is very similar to the complete backpack in the previous question, but there is an unlimited number of purchases here. You can take it wirelessly as long as you judge that s[i]=0.
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<string.h> #include<string> #include<unordered_map> #include<math.h> #include<queue> typedef long long ll; const int N = 10010; ll f[N], v[N], w[N], s[N]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) cin >> v[i] >> w[i] >> s[i]; for (int i = 0; i < m; i++) { for (int j = n; j >= 0; j--) { for (int k = 1; k * v[i] <= j && ((s[i] != 0 && k <= s[i])||!s[i]); k++) { f[j] = max(f[j], f[j - k * v[i]] + k * w[i]); } } } cout << f[n] << endl; return 0; }
Blue Bridge Cup
Time display of previous real questions [ 12th session ][ provincial competition ][ group B ]
#include<iostream> using namespace std; #include<string> typedef long long ll; string get_string(ll num) { if (num == 0)return "0"; string str; while (num) { int a = num % 10; num /= 10; str += a + '0'; } reverse(str.begin(), str.end()); return str; } int main() { string str; long long time; cin >> time; time /= 1000; time -= (time / 86400) * 86400; int hour = time / 3600; if (hour < 10)str += "0"; str += get_string(hour); str += ":"; time -= (time / 3600) * 3600; int mintue = time / 60; if (mintue < 10)str += "0"; str += get_string(mintue); str += ":"; time -= (time / 60) * 60; if (time < 10)str += "0"; str += get_string(time); cout << str; return 0; }