24h購物| | PChome| 登入
2012-12-18 21:49:16| 人氣1,466| 回應0 | 上一篇 | 下一篇

[UVA][greedy+dp] 711 - Dividing up

推薦 0 收藏 0 轉貼0 訂閱站台


 Dividing up 

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 file describes one collection of marbles to bedivided. The lines consist of six non-negative integers $n_1,dots,n_6$,where ni is the number of marbles of value i. So, theexample 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 isthe 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 01 0 0 0 1 10 0 0 0 0 0

Sample Output 

Collection #1:
Can't be divided.

Collection #2:
Can be divided.


把1 2 3 4 5 6 劃分成 {1,2,4}, {3,6,5}
第一集合可以直接用 greedy 湊出某個數字是否可行。
第二集合前兩個可以用 greedy 湊出某個數字是否可行,第三個數字採用 dp 的方式進行。

之後兩個結果並起來使用即可。
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int A[7], i;
int cases = 0;
while(1) {
int sum = 0;
for(i = 1; i <= 6; i++)
scanf("%d", &A[i]), sum += i*A[i];
if(sum == 0)
break;
printf("Collection #%d:\n", ++cases);
if(sum%2) {
puts("Can't be divided.\n");
continue;
}
int hsum = sum/2, n, a, b, c;
char m1[120005] = {}, m2[120005] = {};
for(i = 0; i <= hsum; i++) { // 1 2 4
n = i;
a = A[1], b = A[2], c = A[4];
if(n/4 >= c) n -= 4*c;
else n %= 4;
if(n/2 >= b) n -= 2*b;
else n %= 2;
if(n/1 >= a) n -= 1*a;
else n %= 1;
if(n == 0) m1[i] = 1;
}
if(A[3]) {
a = A[3]*3+A[6]*6;
for(i = 0; i <= a; i += 3)
m2[i] = 1;
} else {
a = A[6]*6;
for(i = 0; i <= a; i += 6)
m2[i] = 1;
}
if(A[5]) {
b = A[5];
for(i = hsum; i >= 0; i--) {// (3 6) 5
if(m2[i]) continue;
n = i;
b = 0;
for(a = 0, b = min(A[5]*5, i/5*5); a <= b && !m2[i]; a += 5, b -= 5) {
if(m2[i-a] || m2[i-b])
m2[i] = 1;
}
}
}
for(i = 0; i <= hsum; i++) {
if(m1[i] && m2[hsum-i])
break;
}
if(i == hsum+1)
puts("Can't be divided.");
else
puts("Can be divided.");
puts("");
}
return 0;
}

台長: Morris
人氣(1,466) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][cycle] 10162 - Last Digit
此分類上一篇:[UVA][greedy] 11292 - Dragon of Loowater

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文