Test Instructions: The value of 1,2,3,4,5,6 items respectively a[1],a[2],a[3],a[4],a[5],a[6], ask can be divided into two piles of equal value. Solution: Convert to multiple backpacks
1#include <stdio.h>2#include <string.h>3#include <algorithm>4#include <iostream>5 using namespacestd;6 7 intdp[120010];8 inta[Ten];9 Ten intNvalue; One A //0-1 backpack, at cost, to get the value of weight - voidZeroonepack (intCostintweight) - { the for(inti=nvalue;i>=cost;i--) -Dp[i]=max (dp[i],dp[i-cost]+weight); - } - + //full backpack, cost, value for weight - voidCompletepack (intCostintweight) + { A for(inti=cost;i<=nvalue;i++) atDp[i]=max (dp[i],dp[i-cost]+weight); - } - - //Multiple Backpack - voidMultiplepack (intCostintWeightintamount) - { in if(cost*amount>=nvalue) Completepack (cost,weight); - Else to { + intk=1; - while(k<amount) the { *Zeroonepack (k*cost,k*weight); $amount-=K;Panax Notoginsengk<<=1; - } theZeroonepack (Amount*cost,amount*weight);//don't forget it, it's often dropped. + } A } the + intMain () - { $ intIcase=0; $ while(1) - { -icase++; the intTol=0; - for(intI=1; i<=6; i++)Wuyi { thescanf"%d",&a[i]); -tol+=i*A[i]; Wu } - if(tol==0) Break; Aboutprintf"Collection #%d:\n", icase); $ if(tol%2!=0) - { -printf"Can ' t be divided.\n\n"); - Continue; A } + Else the { -nvalue=tol/2; $ the } theMemset (DP,0,sizeof(DP)); the for(intI=1; i<=6; i++) the Multiplepack (I,i,a[i]); - if(dp[nvalue]==nvalue) printf ("Can be divided.\n\n"); in Elseprintf"Can ' t be divided.\n\n"); the } the return 0; About}
HDU 1059 Multi-weight backpack