博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
多重背包,附上例题(POJ - 1014 Dividing)
阅读量:4647 次
发布时间:2019-06-09

本文共 3157 字,大约阅读时间需要 10 分钟。

直接上例题

Dividing
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 72067   Accepted: 18813

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.

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.

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 
2 #include
3 #include
4 #include
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 else28 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 }

 

转载于:https://www.cnblogs.com/creativepower/p/7469023.html

你可能感兴趣的文章
R语言 for循环之break,next
查看>>
老生常谈:Windows的7类安全漏洞
查看>>
表单重复提交问题的三种解决思路
查看>>
Windows Phone开发基础(7)《101 Windows Phone 7 Apps》Weight Tracker 提供几种体重发展(折线图 趋势图)...
查看>>
Cookie seesion 赋值
查看>>
winFrom程序更新自动安装
查看>>
Mysql数据类型
查看>>
1.2 日志框架
查看>>
Python 多进程、多线程效率比较
查看>>
设计模式之(十一)代理模式(Proxy)
查看>>
OWC组件生成Excel数据表
查看>>
MySQL基础
查看>>
Largest Rectangle in a Histogram
查看>>
仿QQ右下角弹出可关闭的消息框: 转载
查看>>
ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(七) 之 历史记录查询(时间,关键字,图片,文件),关键字高亮显示。...
查看>>
Unity 游戏框架搭建 (二十三) 重构小工具 Platform
查看>>
软件工程结对作业02
查看>>
【设计模式】策略模式与状态模式。
查看>>
Eclipse经验总结
查看>>
(转)[Unity3D]UI方案及制作细节(NGUI/EZGUI/原生UI系统) 内附unused-assets清除实例
查看>>