题目来源: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. AAC_LC用LATM封装header信息解析 Audio Specific Config格式分析

    通常来说AAC的头信息在编解码过程中是可以获取到的,但今天需要根据音频参数生成相应的AAC头.项目中使用的是AAC_LC,今天先对它的结构进行分析. 项目中使用ffmpeg进行音频编码,音频编码库为F ...

  2. 2017-2018-1 20179203 《Linux内核原理与分析》第五周作业

    攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  3. BZOJ4976: [Lydsy1708月赛]宝石镶嵌

    BZOJ4976: [Lydsy1708月赛]宝石镶嵌 https://lydsy.com/JudgeOnline/problem.php?id=4976 分析: 本来是从\(k\le 100\)这里 ...

  4. BestCoder Round #93 比赛记录

    机房又迎来了一次BC.大家都沸腾了... BC开场,大家全都瞬间开始 啪啦啪啦啪啦啪啦 都要赶紧水过第一题. 第一题明显直接贪心就好了,用map去重. 本人荣幸地第一个写完,提交 Wa. (崩溃的内心 ...

  5. Guava手记

    Cache Guava的Cache封装的功能比较全面,但是很多地方和设想的不太一样,最明显的就是RemovalListener,它并不是invalid之后就会被调用,因为在调用Cache的invali ...

  6. 洛谷【P1480】A/B Problem

    题目传送门:https://www.luogu.org/problemnew/show/P1480 高精除低精板子题,灵性地回忆一下小学时期列竖式的草稿纸即可. 时间复杂度:\(O(len)\) 空间 ...

  7. bzoj 2178 圆的面积并 —— 辛普森积分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2178 先看到这篇博客:https://www.cnblogs.com/heisenberg- ...

  8. 我的日志app企划书1.0版本

    因为个人的工作习惯,想要做一个app,是关于工作(生活)日志的. 目前有几个预想的功能吧. 1.按天展示自己的每日安排. 2.每到周末展示自己的周末安排. 1的需要: 是由于,每天总有那么一点两点的细 ...

  9. a标签中href="javacript:;" href="javacript:void(0);" href="#"区别

    在使用<a>标签时,经常会绑定其他事件比如onclick,这时候我们会给<a>标签的href属性赋值为“#”,“javacript:;”,“javacript:void(0); ...

  10. Python:列表反序和解析

    1)列表反序 A.list.reverse():将列表反序: l = [1, 2, 3, 4, 5] print(l.reverse()) -->[5, 4, 3, 2, 1] B.l.[::- ...