标签:between oss rom max == elves receive ++ pst
直接上例题
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 72067 | Accepted: 18813 |
Description
Input
Output
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1: Can‘t be divided. Collection #2: Can be divided.
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int cnt ; 7 int dp[120004]; 8 void ZeroOne(int v, int w) { 9 for(int i = cnt; i >= v ; i--){ 10 dp[i] = max(dp[i],dp[i - v] + w); 11 } 12 } 13 void complete(int v, int w) { 14 for(int i = v; i <= cnt ; i++){ 15 dp[i] = max(dp[i],dp[i - v] + w); 16 } 17 } 18 void multiply(int v,int w,int num) { 19 if(v*num < cnt){ 20 for(int i = 1; i < num ; ){ 21 ZeroOne(i*v,i*w);//二进制优化 22 num -= i; 23 i <<= 1; 24 } 25 ZeroOne(num*v,num*w); 26 } 27 else 28 complete(v,w); 29 } 30 int main() 31 { 32 int a[10]; 33 int Case = 1; 34 35 while( scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]) != EOF ) { 36 cnt = 0; 37 for(int i = 1; i <= 6; i++) { 38 cnt += a[i]*i; 39 } 40 if(!cnt) break; 41 printf("Collection #%d:\n",Case++); 42 if(cnt%2) { printf("Can‘t be divided.\n\n");continue;} 43 cnt /= 2; 44 memset(dp,0,sizeof(dp)); 45 for(int i =1; i <= 6; i++) { 46 multiply(i,i,a[i]); 47 } 48 if(dp[cnt] == cnt) printf("Can be divided.\n\n"); 49 else printf("Can‘t be divided.\n\n"); 50 } 51 return 0; 52 }
多重背包,附上例题(POJ - 1014 Dividing)
标签:between oss rom max == elves receive ++ pst
原文地址:http://www.cnblogs.com/creativepower/p/7469023.html