HDU 3466 (01 backpack variants

Source: Internet
Author: User

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-&GT;Q-AA-&GT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.