[POJ 1014] Dividing【DFS】

  • 2018-01-01
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 10000K

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.

Source

Mid-Central European Regional Contest 1999

Solution:

这一题本来是多重背包的模板题,乍一看 DFS 会 TLE。但是 DFS 有一个非常有效的可行性剪枝,可以将时间复杂度降到与多重背包相近。

首先判断价值总和是否为偶数,如果不是,直接输出 "Can't be divided."。

DFS 时从大到小枚举,可以更快地找到答案。

每当取走某一价值的大理石的尝试失败时,无需回溯。因为若回溯,在后续尝试中还会取到同一块大理石,肯定会再次失败,不符合可行性。可以视为将该大理石给对方

Code: O(玄学) [320K, 0MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int T = 0, n[7], ave;

inline bool getCollection(){
	for(register int i = 1; i <= 6; i++)
		if(scanf("%d", n + i) == EOF) return 0;
	return 1;
}

inline bool DFS(int tot){
	if(tot == ave) return 1;
	for(register int i = 6; i >= 1; i--)
		if(n[i] && tot + i <= ave){
			n[i]--;
			if(DFS(tot + i)) return 1;
			// Feasibility pruning: not to backdate the n[i], or the program will receive TLE
			/*
				Reason:
				When the attempt of taking value i fails,
				we consider giving this value i to the other person,
				or we must fail again.
			*/
		}
	return 0;
}

int main(){
	while(getCollection() && (n[1] != 0 || n[2] != 0 || n[3] != 0 || n[4] != 0 || n[5] != 0 || n[6] != 0)){
		printf("Collection #%d:\n", ++T);
		ave = n[1] + 2 * n[2] + 3 * n[3] + 4 * n[4] + 5 * n[5] + 6 * n[6];
		if(ave & 1){
			puts("Can't be divided.\n");
			continue;
		}
		ave >>= 1;
		if(DFS(0)) puts("Can be divided.\n");
		else puts("Can't be divided.\n");
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai