题目来源:http://poj.org/problem?id=1014

题目大意:

  Marsha和Bill拥有一些弹珠。但是这些弹珠的价值不一样。每个弹珠的价值都是1到6之间的自然数。他们希望把这些弹珠分为两份,每份的总价值相等。然而,有时候是不存在这样的划分的(即使总的价值为偶数)。比如弹珠的价值分别为1,3,4,4.写一个程序判断一些弹珠是否可以被分为价值相等的两份。

输入:每行代表一个测试用例,含6个非负整数。n1,...n6.ni表示价值为i的弹珠有多少个。测试用例最多20000个。输入以“0 0 0 0 0 0”结束。

输出:对于每个用例,若可分,输出: "Collection #k:", 其中k为用例编号, "Can be divided." 或 "Can't be divided."每个用例输出之后接一个空白行。


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.

首先,如果弹珠的总价值为奇数一定不可分,然后,用dfs搜索,找出所有弹珠中的一个子集,使其和为总价值的一半,若能找到则可分,反之不可分。

 //////////////////////////////////////////////////////////////////////////
// POJ1014 Dividing
// Memory: 596K Time: 0MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int stone[];
int totalValue;
bool flag = false; bool dfs(int sum, int s, int target) {
if (flag) return true;
if (sum == target) {
return true;
}
for (int i = s; i >; --i) {
if (stone[i - ]) {
if (sum + i <= target) {
--stone[i - ];
if (dfs(sum + i, i, target)) return true;
}
}
}
return false;
} int main(void) { int caseNo = ;
while (true) {
totalValue = ;
flag = false;
for (int i = ; i < ; i++) {
cin >> stone[i];
totalValue += stone[i] * (i + );
}
if ((stone[] || stone[] || stone[] || stone[] || stone[] || stone[]) == ) {
break;
}
++caseNo;
//若价值和为奇数,一定不可分
if (totalValue % == ) {
cout<<"Collection #"<<caseNo<<':'<<endl;
cout<<"Can't be divided."<<endl<<endl;
continue;
}
if (dfs(, , totalValue / )) {
cout<<"Collection #"<<caseNo<<':'<<endl;
cout<<"Can be divided."<<endl<<endl;
continue;
}
else {
cout<<"Collection #"<<caseNo<<':'<<endl;
cout<<"Can't be divided."<<endl<<endl;
continue;
}
}
system("pause");
return ;
}

POJ1014 Dividing的更多相关文章

  1. 【DP|多重背包可行性】POJ-1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of mar ...

  2. poj1014 Dividing (多重背包)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=1014">http://poj.org/problem?id=1014 Descrip ...

  3. POJ1014:Dividing(多重背包)

    http://poj.org/problem?id=1014 Description Marsha and Bill own a collection of marbles. They want to ...

  4. [POJ1014]Dividing(二进制优化多重背包)

    #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int ...

  5. hdu1059&poj1014 Dividing (dp,多重背包的二分优化)

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

  6. poj分类解题报告索引

    图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...

  7. 【poj1014】 Dividing

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

  8. POJ1014:Dividing

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63013   Accepted: 16315 Descri ...

  9. poj1014 hdu1059 Dividing 多重背包

    有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...

随机推荐

  1. 【Caffe】Ubuntu 安装 Caffe gpu版

    安装环境:Ubuntu 16.04lts 64位, gcc5.4 gpu1050ti,cuda8.0,cudnn5.1.10 1. 安装依赖库 sudo apt-get install libprot ...

  2. 使用IronPython集成Python和.NET

    本文由 伯乐在线 - 艾凌风 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:www.informit.com.欢迎加入翻译组. 从两个优秀的世界各取所需,更高效的复用代码.想想就醉了,. ...

  3. 洛谷【P2003】平板

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.luogu.org/problemnew/show/P ...

  4. bzoj 2850 巧克力王国——KDtree

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2850 改一下估价即可.判断子树能否整个取或者是否整个不能取,时间好像就能行了? 因为有负数, ...

  5. 用Fiddler2来监听HTTP(记:用skydrive sdk访问时,出错后用Fidder抓包分析)

    最近在写一个关于如何上传文件到skydrive的demo, 用REST上传失败. 安装Telerik的Fiddler后, 可以监听http或者https通信, 然后可以在软件中看到返回的json数据或 ...

  6. Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)

    一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ...

  7. web攻击之八:溢出攻击(nginx服务器防sql注入/溢出攻击/spam及禁User-agents)

    一.什么是溢出攻击 首先, 溢出,通俗的讲就是意外数据的重新写入,就像装满了水的水桶,继续装水就会溢出,而溢出攻击就是,攻击者可以控制溢出的代码,如果程序的对象是内核级别的,如dll.sys文件等,就 ...

  8. java注解的基本知识

    1: 注解:Annotation是一种应用于类.方法.参数.变量.构造器及包生命中的特殊修饰符,是一种由JSR-175标准选择用来描述代码的元数据. Java中如下的4种注解,专门负责新注解的创建: ...

  9. VBS调用并监控记事本进程

    Dim flag flag=true Set WshShell = CreateObject("WScript.Shell") '创建WScript.Shell对象     Set ...

  10. jdbc 新认识

    以前一直用jdbc,没有深入看看,原来jdbc是java自己的接口规范,db厂商按照接口进行开发对应的驱动,jdbc可以获取db中的元信息,执行sql,获取结果,操作db等等.示例如下. public ...