poj 1014 Dividing

ACM比赛整理

共 2606字,需浏览 6分钟

 ·

2021-10-16 09:33

Dividing



Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 81457
Accepted: 21527

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 file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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 collection, 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.




描述

玛莎和比尔收集了一些弹珠。他们想把收集的弹珠平分,这样两人就能平分。如果所有的弹珠都有相同的价值,这就很简单了,因为这样他们就可以把收藏品分成两半。但不幸的是,有些弹珠比其他的更大或更漂亮。所以,玛莎和比尔开始给每个弹珠赋一个值,一个1到6之间的自然数。现在他们想把这些玻璃球分开,这样每一个都能得到相同的总值。不幸的是,他们意识到用这种方法分割玻璃球是不可能的(即使所有玻璃球的总价值是偶数)。例如,如果有一个值为1的弹珠,一个值为3的弹珠和两个值为4的弹珠,那么它们就不能被分割成等价的集合。题目要求你写一个程序来检查弹珠的分配是否公平。


输入

输入文件中的每一行描述了要分割的弹珠集合。这些行包含6个非负整数n1,…因此,上面的例子可以用输入行“1 0 1 2 0”来描述。最大弹珠总数将是20000。

输入文件的最后一行为“0 0 0 0 0 0”;不要处理这条线。


输出

对于每个集合,输出“集合#k:”,其中k是测试用例的编号,然后是“可以被除”或“不能被除”。

在每个测试用例之后输出一个空行。


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.



代码:

#include 
#include

using namespace std;

int f[200001];
int num[200001];

int main()
{
int v[7], test = 0;
while(scanf("%d", &v[1]))
{
memset(f,0,sizeof(f));
int i, sum = v[1];
for(i = 2; i <= 6; i ++)
{
scanf("%d", &v[i]);
sum += v[i] * i;
}
if(!sum)
break;
printf("Collection #%d:\n",++ test);
if(sum % 2)
puts("Can't be divided.\n");
else
{
int k, j, c = 0;
f[0] = 1;
sum /= 2;
for(i = 1; i <= 6; i ++)
if(v[i])
{
for(j = i; j <= sum; j ++)
if(!f[j] && f[j-i] && num[j-i] < v[i])
{
f[j] = 1;
num[j] = num[j-i] + 1;
if(f[sum]) break;
}
if(f[sum])
break;
for(j = i; j <= sum; j ++)
num[j] = 0;
}
if(f[sum])
puts("Can be divided.\n");
else
puts("Can't be divided.\n");
}
}
}


浏览 9
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报