http://acm.hdu.edu.cn/showproblem.php?pid=3466
Http://www.cnblogs.com/andre0506/archive/2012/09/20/2695841.html
This problem has a more restrictive condition QI, can not be purchased below Qi.
The problem-solving idea is to look at the amount of updates, because the limit is the amount of updates.
For example, an item is 5 9, an item is 5 6, on the first backpack when only dp[9],dp[10],..., dp[m], and then to the second backpack, if it is normal, should borrow the front dp[8],dp[7], but now these values are 0, This can result in an error.
So you have to think that only the value of the back to be used before can be obtained, then will not be wrong. Set A:P1,Q1 b:p2,q2, if first a after B, then at least p1+q2 capacity, if the first B after a, at least p2+q1 capacity, then P1+Q2 > p2+q1, after deformation is Q1-P1 < Q2-P2.
Also note that the format of the CMP for Qsort is (const void*,const void*), which returns two value subtraction
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>using namespacestd;#defineMEM (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineDebug printf ("!/n")#defineINF 5050#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long LongConst intMAXN =550;intDp[inf];intN,v,i,j,v,tmp;typedefstruct{ intCi,wi,q;} G G G[MAXN];intcmpConst void*a,Const void*b) {G*AA = (G *) A; G*BB = (G *) b; return(AA->Q-AA->CI)-(bb->q-bb->ci);}voidZeroonepack (intCostintweight) { for(v = v;v>=g[i].q && v>=cost;v--) {Dp[v]=max (dp[v],dp[v-cost]+weight); }}intMain () { while(SF ("%d%d", &n,&v)! =EOF) {MEM (DP,0); MEM (g,0); for(i =1; i<=n;i++) {SF ("%d",&g[i].ci); SF ("%d",&g[i].q); SF ("%d",&g[i].wi); if(g[i].q>V) I--, n--; } qsort (G+1Nsizeof(g[1]), CMP); for(i =1; i<=n;i++) {zeroonepack (G[I].CI,G[I].WI); } PF ("%d\n", Dp[v]); } return 0;}
HDU 3466 (01 backpack variants