Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1059 at first glance, I did not think of the type of backpack, so how do we analyze it as a problem of multiple backpacks? The first thing we can know is that if we want to divide these coins into two equal copies of the value, we can use sum to save the total value, using the total value as the size of the backpack, we can use half of the size of the backpack to hold half of the worth of marbles, of course, the remaining half of the backpack can be loaded with the other half worth the marbles.
Restrictions,
Valuable,
Capacity availableSo we use multiple backpacks. AC code: it is a set of multiple backpack templates. This is a template that I think is better understood. The dynamic equation is actually a template of 10 backpacks, full backpacks. The most important thing is that multiple backpacks
Binary processing.If you are not quite clear about the problem, you can view the package uploaded from the Internet.
# Include <iostream> # include <stdio. h> using namespace STD; int num [7]; int sum; int DP [1, 100000]; int max (int A, int B) {If (A> B) return; elsereturn B;} void zeroonepack (INT cost, int weight) {int I; for (I = sum; I> = cost; I --) DP [I] = max (DP [I], DP [I-cost] + weight);} void completepack (INT cost, int weight) {int I; for (I = cost; I <= sum; I ++) DP [I] = max (DP [I], DP [I-cost] + weight );} void multiplepack (INT cost, int weight, int amount) {If (C Ost * Amount> = sum) {completepack (cost, weight);} else {int K = 1; while (k <= Amount) {zeroonepack (K * cost, K * weight); amount-= K; k <= 1; // * 2} zeroonepack (Amount * cost, amount * weight);} int main () {int I; int case = 1; while (1) {memset (DP, 0, sizeof (DP); memset (Num, 0, sizeof (Num )); sum = 0; for (I = 1; I <= 6; I ++) {scanf ("% d", & num [I]); sum + = num [I] * I;} If (sum = 0) break; printf ("collection # % d: \ n", Case ++ ); if (sum % 2 = 1) {printf ("can't be divided. \ N "); continue;} sum/= 2; for (I = 1; I <= 6; I ++) {If (Num [I] = 0) continue; multiplepack (I, I, num [I]);} If (DP [Sum]! = Sum) // convert it to whether or not half of the value of an item can be loaded into a half-capacity package. Printf ("can't be divided. \ n"); elseprintf ("can be divided. \ n");} return 0 ;}