POJ 1014 Dividing(简单dp)
2015-03-25 23:53
288 查看
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
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.
题意:
分别告知你价值为1,2,3,4,5,6的大理石数目,求是否能将这些大理石分成等价值的两份。
思路:
这是一道简单题。
但是我WA了18发,一开始是不会,后来是被细节制裁了。
题目数据很温柔,但计算过程残暴至极。
很明显我们可以进行剪枝:当总value是奇数是一定不能。
其次我们只要思考能否凑出价值之和为总value一半的大理石。
很简单,6层for循环教OJ做人。
TLE。
好吧,我承认还是要用下dp,我们将总价值的一半设为mid,只要flag[mid] = true即可。
一开始,num[1]之内的flag值均为true,包括flag[1]将价值为1的大理石整堆抱走即可。
而后,在可行基础上加石头,注意价值不能超过mid值。
方法还有很多,待补。
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 eitherCan 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.
题意:
分别告知你价值为1,2,3,4,5,6的大理石数目,求是否能将这些大理石分成等价值的两份。
思路:
这是一道简单题。
但是我WA了18发,一开始是不会,后来是被细节制裁了。
题目数据很温柔,但计算过程残暴至极。
很明显我们可以进行剪枝:当总value是奇数是一定不能。
其次我们只要思考能否凑出价值之和为总value一半的大理石。
很简单,6层for循环教OJ做人。
TLE。
好吧,我承认还是要用下dp,我们将总价值的一半设为mid,只要flag[mid] = true即可。
一开始,num[1]之内的flag值均为true,包括flag[1]将价值为1的大理石整堆抱走即可。
而后,在可行基础上加石头,注意价值不能超过mid值。
[code]/* * @author Novicer * language : C++/C */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; int main(){ lint num[7]; int k = 0; while(1){ k++; cin >> num[1] >> num[2] >> num[3] >> num[4] >> num[5] >> num[6]; lint value_sum = 0; lint num_sum = 0; for( int i = 1 ; i <= 6 ; i++ ){ value_sum += num[i] * i; } if (!(num[1] || num[2] || num[3] || num[4] || num[5] || num[6])) break; if (value_sum % 2 != 0) { printf("Collection #%d:\nCan't be divided.\n\n", k); continue; } bool flag[60000]; lint mid_value = value_sum / 2; memset(flag,false,(mid_value + 1)*sizeof(flag[0])); flag[0] = true; for(int i = 1 ; i <= num[1] ; i++){ flag[i] = true; } int much = num[1]; for( int i = 2 ; i <= 6 ; i++ ){ if(num[i] == 0) continue; for( int j = much ; j >= 0 ; j-- ){ if(flag[j]){ for( int k = 1 ; k <= num[i] && j + k * i <= mid_value ; k++){ if(flag[j + k * i]) break; flag[j + k * i] = true; } } } much += num[i] * i; } if(flag[mid_value]){ printf("Collection #%d:\nCan be divided.\n\n", k); } else{ printf("Collection #%d:\nCan't be divided.\n\n", k); } } return 0; }
方法还有很多,待补。
相关文章推荐
- poj 1014 Dividing(简单dp)
- 【DP|多重背包可行性】POJ-1014 Dividing
- POJ1014 Dividing(DP)
- Dividing - POJ 1014 dp背包
- 20140715 「DP - 多重背包」 POJ 1014 Dividing
- [dp问题] Poj 1014 & Zoj 1149 (Dividing) 解题报告(转)
- 【DP|多重背包可行性】POJ-1014 Dividing
- poj1014 Dividing 多重背包DP+神剪枝
- DFS(DP)---POJ 1014(Dividing)
- POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】
- DFS(DP)---POJ 1014(Dividing)
- [poj 1014]Dividing的DFS解法解读和DP解法
- poj 1014Dividing
- poj 2411(插头DP 简单题)
- POJ 2181简单DP
- POJ 1014 Dividing (动态规划、多重背包)
- zoj 1149 || poj 1014 Dividing(多重背包)
- poj 3671 3670——简单DP
- poj1014——Dividing
- POJ-3624-Charm Bracelet-简单0/1背包、动态规划、DP