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. 查询数据库表大小sql

    SELECT a.name, b.rowsFROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.idWHERE (a.type = ' ...

  2. 面试复习(C++)之冒泡排序

    第一次写技术博客,先只贴代码吧. #include <iostream> using namespace std; void Bubble(int *arr,int len) { int ...

  3. VisualSVN5.0.1补丁原创发布

    VisualSVN5.0.1补丁原创发布

  4. 用PHP抓取淘宝商品的用户晒单评论+图片实例

    为什么想起来做这个功能?是因为前段时间在做一个淘客网站的时候,想到是否能抓取到淘宝商品的买家秀呢?经过一番折腾发现,淘宝商品用户评价信息是通过Ajax来调取的,通过嗅探网址发现,评论数据的请求接口是: ...

  5. CALayer anchorPoint 锚点始终为(0,0)

    objc.io 学习 摘自原处修改 对层的属性详细了解可见这里 @interface ClockFace : CAShapeLayer@property (nonatomic, strong) NSD ...

  6. java基础之 序列化

    一.序列化和反序列化的概念 把对象转换为字节序列的过程称为对象的序列化.       把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬 ...

  7. Android Fragment

    1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...

  8. 移动端1px

    移动端不同尺寸设备dpi不同,会造成1px线条不同程度的缩放,可利用媒体查询device-pixel-ratio,进行不同情况匹配: @media(-webkit-min-device-pixel-r ...

  9. The Pragmatic Programmer Quick Reference Guide

    1.关心你的技艺 Care About Your Craft 如果不在乎能否漂亮地开发出软件,你又为何要耗费生命去开发软件呢? 2.思考!你的工作 Think! About Your Work 关掉自 ...

  10. 关于最少VC号数目的猜想

    [事先说明,实际的虚拟电路的实际物理链路可能同时具有多个VC号,但每段逻辑链路仅有一个VC号,一条完整虚拟路径由多个段组成] 问题描述: 在一个虚拟电路交换网络中,每个路由器的直连链路都有一个独一无二 ...