HDU 1059 01背包+完全背包=多重背包
来源:互联网 发布:suse测试udp端口 编辑:程序博客网 时间:2024/05/20 20:47
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10284 Accepted Submission(s): 2866
Problem Description
Marsha 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.
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.
Input
Each 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.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output
For 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.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0
Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.
题目大意:有六种价值的大理石(价值是:1,2,3,4,5,6),输入依次是价值为1,2,3,4,5,6的大理石的个数,要将大理石按价值平均分配给2个人,但是石头不能劈开,输出能不能平分;看输出示例;
光用多重背包是不行的,肯定超时,这里要加上二进制优化,(看过背包九讲的应该理解);
下面贴个AC代码:
#include <iostream>using namespace std;int f[60002], i, j; void ZeroOnePack(int cost, int worth, int V) //01背包 {for(int j = V; j >= worth; j--)f[j] = max(f[j], f[j-cost]+worth);}void CompletePack(int cost, int worth, int V) //完全背包 {for(int j = worth; j <= V; j++)f[j] = max(f[j],f[j-cost]+worth);}void MultiplePack(int cost, int worth, int V, int M) //多重背包,M为物品件数 {if(M * cost >= V){CompletePack(cost,worth,V);return ;}int k = 1;while(k < M){ZeroOnePack(k*cost, k*worth, V);M = M - k;k = 2*k;}ZeroOnePack(M*cost, M*worth, V);}int main(){int N[7] = {0}, sum,k = 0;while(cin>>N[1]>>N[2]>>N[3]>>N[4]>>N[5]>>N[6]){k++;if(!N[0] && !N[1] && !N[2] && !N[3] && !N[4] && !N[5])break;sum = 0;for(i = 1; i <= 6; i++)sum += i*N[i];if(sum % 2 != 0){printf("Collection #%d:\n",k);printf("Can't be divided.\n\n");continue;}else{sum /= 2;for(i = 0; i <= sum; i++)f[i] = 0;for(i = 1; i <= 6; i++){MultiplePack(i,i,sum,N[i]);}if(f[sum] == sum){printf("Collection #%d:\n",k);printf("Can be divided.\n\n");}else{printf("Collection #%d:\n",k);printf("Can't be divided.\n\n");}}}}
- HDU 1059 01背包+完全背包=多重背包
- 01背包,完全背包, 多重背包
- 01背包,完全背包,多重背包
- 01背包、完全背包、多重背包详解
- 01背包,完全背包,多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包
- 01背包、完全背包、多重背包模板
- 01背包 完全背包 多重背包
- 01背包,完全背包,多重背包详解
- 01背包 完全背包 多重背包
- 01背包、完全背包、多重背包模版。
- 01背包&完全背包&多重背包 - 区别
- 01背包、完全背包、多重背包归纳
- 01背包 完全背包 多重背包
- 01背包,完全背包,多重背包
- Google Map Api 在android 中的使用
- 手机游戏共性分析
- vector C++ 用法
- 其实,我们真的很懒
- 浅议“肾者,胃之关也”
- HDU 1059 01背包+完全背包=多重背包
- 习题 2.64
- struts2 表单的多重递交(Annotation方式)
- 1976-2008年生的人
- SQL查询语言Select用法
- 信息安全成为物联网大发展最关键一环
- 十六个快速启动项目样板
- 根据移动性、云和社交媒体重新思考安全问题
- 明星软件工程师的十种特质