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. Android的RecyclerView

    简介 RecyclerView是support-v7中用来替换ListView的组件.RecyclerView 小组件比 ListView 更高级且更具灵活性. 此小组件是一个用于显示庞大数据集的容器 ...

  2. CDHtmlDialog的基本使用

    转自:http://blog.csdn.net/sky04/article/details/7587406 因为我的部门只有我一个人(无奈之极,只有我一个做C++的,其他的都在做C#),所以我去跟技术 ...

  3. win8没有无线网络适配器问题

    1. 先关闭ssid : 命令提示符(管理员)输入 :netsh wlan set hostednetwork mde=disallow ssid 2. 再设置无线名称和密码 :netsh wlan ...

  4. web开发流程(传智播客-方立勋老师)

    1.搭建开发环境 1.1 导入项目所需的开发包 dom4j-1.6.1.jar jaxen-1.1-beta-6.jar commons-beanutils-1.8.0.jar commons-log ...

  5. LCD底层驱动分析

    根据分析的框架,自己写一个LCD驱动程序 1分析LCD硬件原理图 Von和Voff接的是一个电源电路,通过LCD_POWER接的是GPG4来控制LCD电源,高电平表示开启LCD电源 VM接的是CPU的 ...

  6. if 判断中出现逗号

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. js对象的深度克隆

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Longest Substring Without Repeating Characters(Difficulty: Medium)

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  9. AudioUnit 用法

    1.描述音频单元 AudioComponentDescription desc; desc.componentType = kAudioUnitType_Output; desc.componentS ...

  10. Java数组和C++异同

    一.定义和初始化 1.Jave定义和初始化: Java:两种方式 Type[]  A; Type A[]; 定义时不能指定数组的长度 静态初始化:   A = new Type[][Ele1,Ele2 ...