Dividing

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 66032 Accepted: 17182

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,2,3,4,5,6的6种物品,输入6个数字,数字i代表价值为i的物品有ni个。问能否将物品分成等价的两份(物品当然不能拆开喽)。

输入:

每一行有六个数 分别代表价值为1,2,3,4,5,6的物品的个数,最大物品总数为20000.输入的结束标志为六个0.

输出:

对于第k个样例,输出“Collection #k:”,可以分成等价的两份则输出“Can be divided.”,不可以就输出“Can’t be divided.”。在每个样例后要记得要输出回车。

思路:

0/1背包的二进制优化。。

设一个变量all代表所有物品的价值和。如果all%2==1 则无论怎么分都不可能分成相等的两份。

如果在背包进行的过程中出现了dp[j]==all/2的情况,就可以被分成两份。(如果说的不清楚详情请见代码)。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100],cases=0,dp[666666],f[666666],tot,all,w;
bool flag;
int main()
{
while(1)
{
cases++;
tot=0;flag=false;all=0;
memset(dp,0,sizeof(dp));
memset(f,0,sizeof(f));
for(int i=1;i<=6;i++) {scanf("%d",&a[i]);all+=a[i]*i;}
if(a[1]||a[2]||a[3]||a[4]||a[5]||a[6])
{
for(int i=1;i<=6;i++)
{
int t=1;
while(a[i]>=t)
{
tot++;
a[i]=a[i]-t;
f[tot]=t*i;
t*=2;
}
tot++;
f[tot]=a[i]*i;
}
if(all%2==1)goto end;
else w=all/2;
for(int i=1;i<=tot;i++)
{
for(int j=all;j>=f[i];j--)
{
dp[j]=max(dp[j],dp[j-f[i]]+f[i]);
if(dp[j]==w)flag=true;
}
}
if(!flag) end: printf("Collection #%d:\nCan't be divided.\n\n",cases);
else printf("Collection #%d:\nCan be divided.\n\n",cases);
}
else break;
}
}

POJ 1014 Dividing的更多相关文章

  1. POJ 1014 Dividing(多重背包+二进制优化)

    http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...

  2. DFS(DP)---POJ 1014(Dividing)

    原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...

  3. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  4. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  5. Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)

    多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...

  6. POJ 1014 Dividing (多重可行性背包)

    题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...

  7. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  8. POJ 1014 Dividing 背包

    二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...

  9. POJ 1014 Dividing(入门例题一)

    Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description Marsha and Bill own ...

随机推荐

  1. web app性能大讨论

    1.Application:应用,为用户完成一个或多个功能而设计的程序: 2.Internet or Intranet:运行于广域网或局域网之上: 3.Browser-supported langua ...

  2. JSP内置对象---request对象(用户登录页面(返回值和数组:gerParameter,getParameterValues))

    创建两个jsp页面:reg.jsp 和 request.jsp reg.jsp: <%@ page language="java" import="java.uti ...

  3. Linux学习 :字符设备框架

    一.系统功能框架: U-boot : 启动内核 linux kernel: 启动应用 应用: open,read,write 都是通过C库实现,汇编就相当于swi val,引发中断,通过系统调用接口在 ...

  4. python取文件最后几行

    with open("text.txt") as f:     txt=f.readlines() keys=[k for k in range(0,len(txt))] resu ...

  5. selenium+python+eclipse开发中遇到的问题

    1.中文编码问题 报错提示:SyntaxError: Non-ASCII character '\xba' in file D:\autotest\PythonCase\src\selenium\te ...

  6. Dojo的Gridx使用jsonrest需要注意的地方

    在使用gridx时,如果要使用jsonrest,主要的工作主要是在服务端,服务端在返回数据时,必须在返回头里添加Content-Range: 0-9/73 属性和值,其中0表示从第0条记录开始,9表示 ...

  7. sqlite学习

    一鼓作气,今天继续学习了sqlite数据库在Xcode上的一些操作,主要是通过用oc代码进行salite表格的创建,删除,修改:以及对现有的表格数据进行增,删,改,查.虽然有点累,但是收获不小,感觉很 ...

  8. Linux下swoole的安装配置

    前几天搭建swoole环境,在安装php的swoole扩展时不知道什么原因,提示成功,但是使用的时候不能加载,最后决定重新安装php试试,顺便记录了php的安装过程 wget http://cn2.p ...

  9. 7.2.3 使用RenderTargetBitmap类生成图片

    RenderTargetBitmap类可以将可视化对象转换为位图,也就是说它可以将任意的UIElement以位图的形式呈现.那么我们在实际的编程中通常会利用RenderTargetBitmap类来对U ...

  10. AC自动机及trie图 pascal

    ; type data=record sum,failed:longint; son:array ['a'..'z'] of longint; end; ..maxn] of data; que:.. ...