A cow is going to go to space. He has many kinds of stones. The height of each kind of stone is hi, but it cannot be placed on the height of ai, and this kind of stone has ci
Combine these stones and ask the highest possible height.
Solution: first, Sort data in ascending order, which is a standard problem of multiple backpacks.
Why sort?
Because only in this way can we get the optimal solution. If it is higher at the beginning, there will be lower but not higher options at the end, so we can directly choose higher ones. In this way, the optimal solution cannot be achieved.
Indicates whether the State mark of f [I] can reach this height.
In this way, the maximum I value in f [I] can be obtained.
Note that when assigning an initial value to max, the value must be 0, not-1, because the answer may be 0.
#include <iostream>#include <cmath>#include <cstring>#include <cstdio>#include <cstdlib>#include <algorithm>using namespace std;int type;int f[44444],usr[44444];struct Block{int h,a,c;}block[555];bool cmp(Block a,Block b){return a.a<b.a;}int main(){scanf("%d",&type);for(int i=1;i<=type;i++){scanf("%d%d%d",&block[i].h,&block[i].a,&block[i].c);}sort(block+1,block+1+type,cmp);int maxn=0;f[0]=1;for(int t=1;t<=type;t++){memset(usr,0,sizeof(usr));for(int h=block[t].h;h<=block[t].a;h++){if(!f[h] && f[h-block[t].h] && usr[h-block[t].h]+1<=block[t].c){usr[h]=usr[h-block[t].h]+1;f[h]=1;maxn=max(h,maxn);}}}printf("%d\n",maxn);return 0;}