ACM 1014 Dividing 背包问题
来源:互联网 发布:mac os x 隐藏文件夹 编辑:程序博客网 时间:2024/06/16 13:54
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 54590 Accepted: 13973
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.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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 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.
多重背包问题,详细看背包九讲
Source CodeProblem: 1014Memory: 488KTime: 16MSLanguage: C++Result: AcceptedSource Code#include <iostream>using namespace std;const int MAX=120000;const int len=7;int f[MAX];int n[len];int V;const int init=-9999;int max(int a,int b){ return a>b?a:b;}void zeroOnePack(int cw){ for(int v=V;v>=cw;v--) f[v]=max(f[v],f[v-cw]+cw);}void completePack(int cw){ for(int v=cw;v<=V;v++) f[v]=max(f[v],f[v-cw]+cw);}void multiplePack(int cw,int amount){ if(cw*amount>V) { completePack(cw); return; } int k=1; while(k<amount) { zeroOnePack(k*cw); amount=amount-k; k=k*2; } zeroOnePack(amount*cw);}int main(){ int count=0; while(1) { count++; int i; int bin=0; int sum=0; for(i=1;i<len;i++) {cin>>n[i]; bin|=n[i];sum+=i*n[i];} if(bin==0) break; if(sum%2!=0) cout<<"Collection #"<<count<<":\n"<<"Can't be divided."<<endl<<endl; else { sum/=2; V=sum; f[0]=0; for(i=1;i<=V;i++) f[i]=init; for(i=1;i<len;i++) multiplePack(i,n[i]); if(f[V]!=V) cout<<"Collection #"<<count<<":\n"<<"Can't be divided."<<endl<<endl; else cout<<"Collection #"<<count<<":\n"<<"Can be divided."<<endl<<endl; } } return 0;}
- ACM 1014 Dividing 背包问题
- HDU1059 Dividing 多重背包 ACM
- POJ 1014 Dividing (多重背包问题)
- poj 1014 Dividing 多重背包问题
- Dividing 多重背包问题
- 1014 Dividing 多重背包
- POJ 1014 Dividing 背包
- Dividing----POJ_1014----多重背包问题
- Dividing 03多重背包问题
- Dividing+POJ+01背包问题
- HDU1059 Dividing(背包问题)
- POJ 1014 && HDU 1059 Dividing(完全背包问题)
- POJ 1014 Dividing (多重背包问题+递归)【模板】
- poj 1014 Dividing 多重背包
- PKU-1014 Dividing (多重背包)
- Poj 1014 Dividing//多重背包
- Poj 1014 Dividing(多重背包)
- POJ 1014 Dividing 多重背包
- error: crosses initialization of
- SpringMVC 初接触
- 设计模式(一) 观察者模式、装饰模式、工厂模式
- .ftl ckeditor刷新时无法重新加载,无法获取改变后textarea的值
- C语言 - 斐波那契数列(Fibonacci) 指定区间输出
- ACM 1014 Dividing 背包问题
- 回顾:让人崩溃的GOP(下)——HEVC及其RPS
- 安装完Rational Rose报错Java.lang.NullPointerException
- 创建守护进程
- 2013级C++第16周项目——初识指针
- 第十章 面向对象 简答题4
- Android中退出多个Activity的方法
- sqlite3的一些常用语句
- SDN文献阅读