Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20635 Accepted Submission(s): 5813Problem DescriptionMarsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.InputEach line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.OutputFor each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.
Output a blank line after each test case.Sample Input1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0Sample OutputCollection #1:Can't be divided.Collection #2:Can be divided.Source
哎~dp好难..........
题意:每行六个数,若都为零则结束,否则,第几个数代表价值为几的东西有几个,东西数量不超过20万,问所有东西能不能分成两堆价值相等的。
能的话输出 Can..,否则Can't...,每组输出后面有个空行~
多重背包加优化..暂时还是不能深入的理解dp的奥义..烦恼ing...
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7 #include <climits> 8 #include <queue> 9 #define ll long long 10 11 using namespace std; 12 int dp[120005],w[10]; 13 int main(void) 14 { 15 int ans,sum,cnt = 1; 16 while(scanf("%d",&w[1]) != -1) 17 { 18 sum = 0; 19 ans = 0; 20 if(w[1] != 0) ans= 1; 21 sum += w[1]; 22 for(int i = 2; i <= 6; i++) 23 { 24 scanf("%d",&w[i]); 25 sum += w[i]*i; 26 if(w[i]) 27 ans = 1; 28 } 29 if(!ans) 30 break; 31 if(sum % 2 == 1) 32 { 33 printf("Collection #%d: Can't be divided. ",cnt++); 34 } 35 else 36 { 37 memset(dp,0,sizeof(dp)); 38 for(int i = 0; i <= w[i] && i <= sum/2; i++) dp[i] = 1; 39 for(int i = 2; i <= 6; i++) 40 for(int j = sum/2; j >= 0; j--) 41 { 42 if(dp[j] == 0) 43 continue; 44 for(int k = 1; k <= w[i] && k *i+j <= sum/2; k++) 45 { 46 if(dp[k*i+j]) break; 47 dp[k*i+j] = 1; 48 } 49 } 50 if(dp[sum/2] == 1) 51 printf("Collection #%d: Can be divided. ",cnt++); 52 else 53 printf("Collection #%d: Can't be divided. ",cnt++); 54 } 55 56 57 } 58 return 0; 59 }