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. flume学习

    下载 自定义sink(mysql) 1.ide打开下载后的源码 2.代码如下: /** * Licensed to the Apache Software Foundation (ASF) under ...

  2. 数对的个数(cogs610)

    Description出题是一件痛苦的事情!题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A- ...

  3. Mysql之INFORMATION_SCHEMA解析1

    INFORMATION_SCHEMA库是Mysql提供的一个系统库,保存了数据库的原数据,方便用户监控与管理Msyql. 现在单说与INNODB相关的库:INNODB_SYS_TABLES,INNOD ...

  4. C 替换字符方法

    // 444.cpp : Defines the entry point for the console application. // #include "stdafx.h" # ...

  5. FastPolice项目总结

    This is the final homework for spatial information Mobile Service Lesson.It generally inclusived the ...

  6. stat file 查看文件的 最新的被访问时间 最近的修改时间 最近的状态改变时间

    [root@NB ~]# stat /media/6FE5-D831/git-data/IT-DOC/web收藏.txt File: `/media/6FE5-D831/git-data/IT-DOC ...

  7. Linq to json

    Json.Net系列教程 4.Linq To JSON 一.Linq to JSON是用来干什么的? Linq to JSON是用来操作JSON对象的.可以用于快速查询,修改和创建JSON对象.当JS ...

  8. Android Studio 配置

    Android配置:[转]原地址:http://www.cnblogs.com/smyhvae/p/4022844.html [开发环境] 物理机版本:Win7旗舰版(64位) Android Stu ...

  9. 攻城狮在路上(壹) Hibernate(十一)--- 映射实体关联关系

    本文以Customer和Address类的关系为例说明一对一关联映射:以Category和Item类的关系说明多对多关联关系.一.映射一对一关联: 分两种情况:按照外键映射和按照主键映射.这两种方式的 ...

  10. LeetCode39/40/22/77/17/401/78/51/46/47/79 11道回溯题(Backtracking)

    LeetCode 39 class Solution { public: void dfs(int dep, int maxDep, vector<int>& cand, int ...