POJ1014 Dividing
题目来源: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的更多相关文章
- 【DP|多重背包可行性】POJ-1014 Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Description Marsha and Bill own a collection of mar ...
- poj1014 Dividing (多重背包)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=1014">http://poj.org/problem?id=1014 Descrip ...
- POJ1014:Dividing(多重背包)
http://poj.org/problem?id=1014 Description Marsha and Bill own a collection of marbles. They want to ...
- [POJ1014]Dividing(二进制优化多重背包)
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; int ...
- hdu1059&poj1014 Dividing (dp,多重背包的二分优化)
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
- poj分类解题报告索引
图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Jou ...
- 【poj1014】 Dividing
http://poj.org/problem?id=1014 (题目链接) 题意 给出有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份, ...
- POJ1014:Dividing
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63013 Accepted: 16315 Descri ...
- poj1014 hdu1059 Dividing 多重背包
有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...
随机推荐
- freeMarker(八)——程序开发指南之配置(Configuration)
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.基本内容 配置(configuration)就是 freemark ...
- 【JVM】jvm垃圾回收器相关垃圾回收算法
引用计数法[原理]--->引用计数器是经典的也是最古老的垃圾收集防范.--->实现原理:对于对象A,只要有任何一个对象引用A,则计数器加1.当引用失效时,计数器减1.只要对象A的计数器值为 ...
- [Codeforces 204E] Little Elephant and Strings
[题目链接] https://codeforces.com/contest/204/problem/E [算法] 首先构建广义后缀自动机 对于自动机上的每个节点 , 维护一棵平衡树存储所有它所匹配的字 ...
- Oracle 12c 多租户在 CDB 中 Plug A PDB,Unplugging A PDB
Oracle 数据库 12 c 中介绍了多租户选项允许单个容器数据库 (CDB) 来承载多个单独的可插拔数据库 (PDB).本文简单的演示了如何在 CDB 中 Plug A PDB,Unpluggin ...
- vmware station中 UDEV 无法获取共享存储磁盘的UUID,症状: scsi_id -g -u -d /dev/sdb 无返回结果。
1.确认在所有RAC节点上已经安装了必要的UDEV包 [root@11gnode1 ~]# rpm -qa|grep udevsystem-config-printer-udev-1.1.16-25. ...
- mouseout与mouseleave的区别
1 mouseout:当鼠标指针从元素上移开时,发生 mouseout 事件.该事件大多数时候会与 mouseover 事件一起使用. 2 mouseout与 mouseleave 事件不同,不论鼠标 ...
- spark减少提交jar包处理
spark一个应用,算上依赖一百多兆.每一次都如此,坑. 首先是<packing>jar</packing>这只为打包为jar,在plugin中增加一个assembly插件,这 ...
- from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import Select Select(d.find_element_by_id(u'key_开户行')).first_sele ...
- 人物-IT-马云:马云
ylbtech-人物-IT-马云:马云 马云 (阿里巴巴集团创始人) 马云,男,汉族,中共党员,1964年9月10日生于浙江省杭州市,祖籍浙江省嵊州市谷来镇, 阿里巴巴集团主要创始人,现担任阿里巴巴集 ...
- java注解的基本知识
1: 注解:Annotation是一种应用于类.方法.参数.变量.构造器及包生命中的特殊修饰符,是一种由JSR-175标准选择用来描述代码的元数据. Java中如下的4种注解,专门负责新注解的创建: ...