One Bentong 1269 Limited Backpack

【Title description】

In order to celebrate the class's first place in the school sports meeting, the class teacher decided to open a celebration party, and for this reason, he will allocate funds to buy prizes to reward the athletes. It is expected that the amount of grants can buy the most valuable prizes, which can supplement their energy and physical strength.

【Enter】

The first two numbers n (n≤500), m (m≤6000), where n represents the number of prizes you want to buy, and m represents the amount of grant.

In the next n rows, there are 3 numbers in each row, v, w, and s, respectively representing the price, value (price and value are different concepts) of the first prize and the maximum number of purchases (both 0 to s Yes), where v≤100, w≤1000, s≤10.

【Output】

One line: a number indicating the maximum value that can be obtained from this purchase (note! Not the price).

【Input example】

5 1000
80 20 4
40 50 9
30 50 7
40 30 6
20 20 1

[Sample output]

1040 

idea: In fact, this question can completely change the code of the full backpack. Just change the conditions in the loop to a few pieces, but then the complexity is O (n ^ 3), which is a bit slow, so improve the code.

The idea of ​​this split is very clever (it is also possible that I am listening for the first time so I feel smart), split a number into the nth power of 2 and another number, for example, 33, 33 = 1 + 32 = 1 + 2 + 30 = 1 + 2 + 4 + 26 = 1 + 2 + 4 + 8 + 18 = 1 + 2 + 4 + 8 + 16 + 2, split this number, and then add any number of them, You will find that the sum of these numbers can take any value from 1 to 33, so that the time complexity can be greatly simplified. First

code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <bits / stdc ++. h>
using namespace std;
int f [5000], v [510], w [510], k [510], v_ [5000], w_ [5000]; // The array size can be opened to n * log2 (m)
int n, m, cnt;
int main ()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++)
cin >> v [i] >> w [i] >> k [i]; // Enter
for (int i = 1; i <= n; i ++)
{
int r = 1; // 2 to the 0th power
while (r <= k [i]) // If it can be demolished, then continue
{
cnt ++; // cnt this variable The meaning of can actually be understood as a category. For example, two items of the first type are tied together to become an item, because this item can be obtained by adding any number to other items, so this is not The problem of
v_ [cnt] = v [i] * r; // The new array is the data after binding
w_ [cnt] = w [i] * r;
k [i]-= r; // Do n’t forget -r
r = r * 2; // 2 next time
}
if (k [i]! = 0) // If there is still left at this time, then open another one and save it in
{
cnt ++;
v_ [cnt] = v [ i] * k [i];
w_ [cnt] = w [i] * k [i];
k [i] = 0;
}
}
for (int i = 1; i <= cnt; i ++) // loop, Starting from the first kind of loop
for (int j = m; j> = v_ [i]; j--) // In fact, the following is the operation of 01 backpack
f [j] = max (f [j], f [j- v_ [i]] + w_ [i]);
cout << f [m];
return 0;
}

Guess you like

Origin www.cnblogs.com/57xmz/p/12735049.html