博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1014 Dividing (多重背包)
阅读量:5992 次
发布时间:2019-06-20

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

转载请注明出处:

题目链接:

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.

Source

大致题意:

有分别价值为1,2,3,4,5,66种物品,输入6个数字。表示对应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等。当中一个物品不能切开,仅仅能分给当中的某一方,当输入六个0是(即没有物品了),这程序结束。总物品的总个数不超过20000

 

输出:每一个測试用例占三行:

           第一行: Collection #k: k为第几组測试用例

           第二行:能否分(详细形式见用例)

           第三行:空白(必须注意,否则PE

#include 
#include
#define INF 100000000 using namespace std;int f[240005]; //f[j]相当于f[i][j]: 考虑1...i个物品。恰好放到容量为j。所能达到的最大价值 int v; //背包容量 int max(int a,int b){ if(a > b) return a; return b;}//处理一个全然背包 (该种物品不限量)void complete_pack(int *a, int c, int w) { for(int i = c; i <= v; i++) a[i] = max(a[i], a[i - c] + w); } //处理一个 01背包 (该种物品仅仅有一个) void zeroone_pack(int *a, int c, int w) { for(int i = v; i >= c; i--) a[i] = max(a[i], a[i - c] + w); } //处理一个多重背包 (该种物品指定上限) void mutiple_pack(int *a, int c, int w, int m) { //若该种物品足以塞满背包-->转化为全然背包 if(c * m >= v) { complete_pack(a, c, w); return; } /*二进制思想拆分:多重背包中的一个物品--变成-->0-1背包中的多个物品 容量:2^0 2^1 2^2 2^k m-∑前面 ***保证k达到最大值 价值:2^0*c 2^1*c 2^2*c 2^k*c (m-∑前面 )*c */ int k = 1; while(k <= m) { zeroone_pack(a, k * c, k * w); m = m - k; k = 2 * k; } } int main() { //freopen("d:/data.in","r",stdin); //freopen("d:/data.out","w",stdout); int sum, i, c[7], w[7], m[7],cas = 0; while(scanf("%d%d%d%d%d%d", &m[1], &m[2], &m[3], &m[4], &m[5], &m[6])) { if(m[1] == 0 && m[2] == 0 && m[3] == 0 && m[4] == 0 && m[5] == 0 && m[6] == 0) break; sum = 0; for(i = 1; i <= 6; i++) { c[i] = w[i] = i; sum += c[i] * m[i]; } printf("Collection #%d:\n", ++cas); if(sum %2 == 1) { printf("Can't be divided.\n\n"); } else { sum /= 2; v = sum; for(i = 1; i <= sum; i++) f[i] = -INF; f[0] = 0; for(i = 1; i <= 6; i++) mutiple_pack(f, c[i], w[i], m[i]); if(f[v] != v)//事实上仅仅要f[v]有正值就能够 { printf("Can't be divided.\n\n"); } else { printf("Can be divided.\n\n"); } } } return 0; }

你可能感兴趣的文章
[VC] 检测AVX系列指令集的支持级别(AVX、AVX2、F16C、FMA、FMA4、XOP)
查看>>
分析与设计数据库模型的简单过程
查看>>
C语言Makefile文件使用
查看>>
C#编程(十三)----------方法重载
查看>>
未安装Silverlight托管包
查看>>
GentleNet使用之详细图解[语法使用增强版]
查看>>
ios将摄像头捕获的视频数据转为jpeg格式
查看>>
php进程的SIGBUS故障
查看>>
telnet测试制定地址端口号
查看>>
保持Service不被Kill掉的方法--双Service守护 && Android实现双进程守护
查看>>
android 截取指定位置字符串
查看>>
李洪强iOS开发之initWithFrame,initWithCoder和aweakFormNib
查看>>
Android ActivityManager.killBackgroundProcesses方法去结束
查看>>
数据库设计原则(转载)
查看>>
MySQL 触发器简单实例
查看>>
Elasticsearch基本概念及核心配置文件详解
查看>>
一次使用Python连接数据库生成二维码并安装为windows服务的工作任务
查看>>
ios_基础篇1_关键字(strong和weak)
查看>>
PageControl
查看>>
我的友情链接
查看>>