ACM学习历程—HDU 1059 Dividing(dp && 多重背包)
2015-05-20 17:14
316 查看
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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., 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 colletcion, 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.
题目大意就是判断这么多数字能不能均分成两类,每个数字不可拆分。
看完题目就感觉是个多重背包。不过这里只用判断sum/2能否被装到。于是就不用判断sum/2+1到sum的背包了。
由于只用判断是否能装到,于是只用开bool型数组即可。
由于每个数字有一定的使用次数,所以需要开vis数组,而且对于每一种数字的背包需要初始化全为1。
由于考虑到需要最优解,所以对于dp[j]为真的情况,就不需要再判断dp[j-i]了,因为如果再靠dp[j-i]来放下num[i]的话,就浪费了一次数字i的使用。
代码:
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 describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., 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 colletcion, 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.
题目大意就是判断这么多数字能不能均分成两类,每个数字不可拆分。
看完题目就感觉是个多重背包。不过这里只用判断sum/2能否被装到。于是就不用判断sum/2+1到sum的背包了。
由于只用判断是否能装到,于是只用开bool型数组即可。
由于每个数字有一定的使用次数,所以需要开vis数组,而且对于每一种数字的背包需要初始化全为1。
由于考虑到需要最优解,所以对于dp[j]为真的情况,就不需要再判断dp[j-i]了,因为如果再靠dp[j-i]来放下num[i]的话,就浪费了一次数字i的使用。
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #define N 1000000007 using namespace std; int num[7], sum; int vis[60005]; bool dp[60005]; bool Input() { sum = 0; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= 6; ++i) { scanf("%d", &num[i]); sum += i*num[i]; } if (sum) return true; else return false; } void Work() { if (sum % 2) { printf("Can't be divided.\n\n"); return; } sum /= 2; dp[0] = true; for (int i = 1; i <= 6; ++i) { if (num[i] == 0) continue; memset(vis, 0, sizeof(vis)); for (int j = i; j <= sum; j++) { if (dp[j-i] && !dp[j] && vis[j-i] < num[i]) { dp[j] = true; vis[j] = vis[j-i]+1; } } } if (dp[sum]) printf("Can be divided.\n\n"); else printf("Can't be divided.\n\n"); } int main() { //freopen("test.in", "r", stdin); int times = 1; while (Input()) { printf("Collection #%d:\n", times); Work(); times++; } return 0; }
相关文章推荐
- ACM学习历程—HDU 5451 Best Solver(Fibonacci数列 && 快速幂)(2015沈阳网赛1002题)
- HDU1059 && POJ1014 :Dividing(多重背包)
- hdu1059 Dividing 我的ACM我的梦,回顾篇 完全背包
- ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)
- POJ 1014 && HDU 1059 Dividing(完全背包问题)
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
- ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)
- hdu 1059 Dividing--DP-多重背包问题
- DP(完全背包二进制优化) Problem T:Dividing(HDU 1059)
- ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)
- http://acm.hdu.edu.cn/showproblem.php?pid=1059&&多重背包
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
- ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
- HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)
- ACM学习历程—HDU1584 蜘蛛牌(动态规划 && 状态压缩 || 区间DP)
- 【日常学习】【多重背包】【二进制优化】hdu1059 Dividing题解
- ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)
- hdu 1059 Dividing DP,多重背包 测试数据很水
- ACM学习历程—HDU5587 Array(数学 && 二分 && 记忆化 || 数位DP)(BestCoder Round #64 (div.2) 1003)
- HDU 1059 Dividing(dp多重背包)