Dividing
 

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. 这道题目多重背包入手真心很简单,题目要求,将弹珠根据价值的大小均分给两个人。
多重背包的模板题目里,问题的相关条件有:背包的体积、 物品的种类、 每种物品的数量、 每种物品所占的体积。这是通常情况,而这道题目里,只有
物品的种类、 每种物品的数量、 每种物品所占的体积这3个条件,但是题目也要求简单,就是看这堆弹珠是否能够均分,所以,背包的体积你可以当作是题目极限条件那么大。
然后运用二进制的思想写出多重背包就好.其实我也就是昨天才学会了多重背包。
 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream> using namespace std;
const int max_size = * + ;
int main()
{
int val[];//val数组里存放每种弹珠的数量
int dp[max_size];//dp数组开题目极限那么大 int cas = ; while(cas)
{
int tag = ;
for(int i = ; i < ; i++)
{
scanf("%d", val+i);
if(val[i] == )
{
tag++;
}
}
memset(dp, , sizeof(dp));
if(tag == )
break;
else
{
int tot = ;
for(int i = ; i < ; i++)
{
tot += val[i] * (i+);
}
int half = tot / ;
int half1 = tot - half;
if(half == half1)
{
bool flag = false;
for(int i = ; i < ; i++)
{
int k = ;
while(k < val[i])
{
for(int j = max_size; j - (i+)*k>= ; j--)
{
dp[j] = max(dp[j], dp[j-(i+)*k]+(i+)*k);
if(dp[j] == half)//在dp过程中,找寻是否有一种状态,满足将弹珠平分这一条件
{
flag = true;
break;
}
}
val[i] -= k;
k *= ;
if(flag == true)
break;
}
if(flag != true)
{
for(int j = max_size; j - val[i]*(i+) >= ; j--)
{
dp[j] = max(dp[j], dp[j-(i+)*val[i]]+(i+)*val[i]);
if(dp[j] == half)
{
flag = true;
break;
}
}
}
else
{
printf("Collection #%d:\n", cas);
printf("Can be divided.\n");
break;
}
}
if(flag != true)
{
printf("Collection #%d:\n", cas);
printf("Can't be divided.\n");
}
}
else
{
printf("Collection #%d:\n", cas);
printf("Can't be divided.\n");
}
}
cas++;
printf("\n");//GG,我去,因为没看要多输出一行空行,PE一次
} return ;
}

 

POJ 1014 Dividing(多重背包)的更多相关文章

  1. 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 ...

  2. POJ 1014 Dividing 多重背包

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

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

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

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

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

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

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

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

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

  7. hdu 1059 Dividing(多重背包优化)

    Dividing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  9. POJ 2392【多重背包】

    题意: k个块,给出每个块的高度hi,数量ci,不能超过的高度: 求这些块可以组成的最大高度一个. 思路: 大致可看这个题是一个背包,背包的承重是高度. 对于每个物品,有他的价值是高度,还有限定的数量 ...

随机推荐

  1. 数据存储-CoreData总结

    CoreData /*英译  Entity:实体 Attributes:属性 binary:二进制 persistent:持续化 coordinator:协调者 meging:合并 configura ...

  2. NYOJ题目1080年龄排序

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtMAAAJVCAIAAACTf+6jAAAgAElEQVR4nO3dO1Lj3NbG8W8Szj0QYg ...

  3. Loadrunner连接Mysql数据库

    1.库文件下载地址:http://files.cnblogs.com/files/xiaoxitest/MySQL_LoadRunner_libraries.zip 分别添加到Loadrunner b ...

  4. hdu 1257 最少拦截系统

    #include<time.h> #include <cstdio> #include <iostream> #include<algorithm> # ...

  5. Mac系统下使用VirtualBox虚拟机安装win7--第四步 安装虚拟机硬件扩展包支持

    如 果想要在虚拟机上使用连接在 Mac 上的硬件外设,比如 U 盘,iPhone 等,需要我们在 Virtual Box 官网下载一个硬件支持扩展安装包.同样地,我们先打开虚拟机的下载页面: http ...

  6. 三、jQuery--jQuery基础--jQuery基础课程--第4章 jQuery表单选择器

    1.:input表单选择器 如何获取表单全部元素?:input表单选择器可以实现,它的功能是返回全部的表单元素,不仅包括所有<input>标记的表单元素,而且还包括<textarea ...

  7. Clr Via C#读书笔记----基元线程同步构造

    线程文章:http://www.cnblogs.com/edisonchou/p/4848131.html 重点在于多个线程同时访问,保持线程的同步. 线程同步的问题: 1,线程同步比较繁琐,而且容易 ...

  8. C#关键字params

    using System; using System.Threading; namespace Test { /// <summary> /// params用法: 1.用来修饰方法的参数 ...

  9. 为什么是 n(n+1)/2 ?

    n(n+1)/2是一个数列的元素两两运算后的不重复结果数.如图: 假如数列a = 1,2,3....n.那么该数列内的元素两两相乘,则会构建出上图中的表格,这个表格应该有n x n 个元素. 用程序写 ...

  10. 提高WPF程序性能的几条建议

    这篇博客将介绍一些提高WPF程序的建议(水平有限,如果建议有误,请指正.) 1. 加快WPF程序的启动速度: (1).减少需要显示的元素数量,去除不需要或者冗余的XAML元素代码. (2).使用UI虚 ...