多重背包
来源:互联网 发布:淘宝的模特是怎么找的 编辑:程序博客网 时间:2024/06/06 14:26
poj:
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 68795 Accepted: 17962
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.
/*************************************************************************> File Name: code/poj-1185.cpp> Author: Zz> Created Time: 2016年09月19日 星期一 22时17分06秒 ************************************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<set>#include<vector>#include<list>using namespace std;bool dp[50*10000];int need[20*10000];int main(){ int zu[7],k=1; while(~scanf("%d",&zu[1])) { int cnt=zu[1]; for(int i=2;i<=6;i++) { scanf("%d",&zu[i]); cnt+=(zu[i]*i); } if(cnt==0) return 0; printf("Collection #%d:\n",k++); if(cnt%2) { printf("Can't be divided.\n\n"); continue; } int num=0,M=(cnt>>1); for(int i=1;i<=6;i++) if(zu[i]) { int bit=1; while(zu[i]>bit) { need[num++]=i*bit; zu[i]-=bit; bit<<=1; } need[num++]=i*zu[i]; } memset(dp,0,sizeof(dp)); dp[0]=true;// for(int i=0;i<num;i++) for(int j=M;j>=need[i];j--) if(!dp[j]&&dp[j-need[i]]) dp[j]=true;// if(dp[M]) printf("Can be divided.\n\n"); else printf("Can't be divided.\n\n"); }}
0 0
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 【多重背包】
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- 多重背包
- NVIDIA显卡历代架构
- hjr学习-聚类分析:K均值
- SQL中Group By的使用
- @interface 自定义annotation 通过AOP来实现人员操作日志
- 导航栏(Navigation)上多个点击事件
- 多重背包
- 将jar包引入到指定的文件夹下
- 自定义View经常重写的方法
- 开发微信公众平台群发接口时,在对接“上传图文消息素材”时,返回44003
- Struts2学习笔记---拦截器实例两个
- Java 多线程知识总结
- C#winform直接连接SQL数据库mdf文件
- 直播从原理到落地开源项目-AnyRTC
- 第四周 项目6 多项式求和