题目来源: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. 【leetcode刷题笔记】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  2. 2017.10.2北京清北综合强化班DAY2

    a[问题描述]你是能看到第一题的 friends呢.                                                —— hja世界上没有什么比卖的这 贵弹丸三还令人绝 ...

  3. 洛谷【P1303】A*B Problem

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

  4. POJ2069:Super Star

    我对模拟退火的理解:https://www.cnblogs.com/AKMer/p/9580982.html 我对爬山的理解:https://www.cnblogs.com/AKMer/p/95552 ...

  5. vijos1782:借教室

    描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...

  6. 【总结整理】overflow: auto/hidden;清除自己

    .top-nav{ font-size: 12px; font-weight: bold; list-style-type: none; border-bottom: 8px solid #DC4E1 ...

  7. Maven Cargo 远程部署到tomcat7x

    pom.xml中加入cargo的Plugin声明: <plugin> <groupId>org.codehaus.cargo</groupId> <artif ...

  8. crontab简易入门

    前言 crontab是Unix和Linux用于设置周期性被执行的指令,是互联网很常用的技术,很多任务都会设置在crontab循环执行,如果不使用crontab,那么任务就是常驻程序,这对你的程序要求比 ...

  9. 【jeasyui5】样式:调整页面显示的顶部菜单和左侧菜单

    1.顶部菜单修改:修改index2.js里面的InitTopMenu方法,将icon +2 2.左侧菜单宽度调整: 修改index.html,加上width:170的定长 <!-- 左侧菜单 - ...

  10. Linux中Root用户密码变更、密码忘记

    用户设置bash的时候,错把root的bash改为bin/bash,注意,不是“/bin/bash”!. 然后就登录不了root了,也修改不了/etc/passwd了. 解决: 1.重启Ubuntu, ...