poj1014:Dividing

可以转化为背包问题,即用一个容量为总数的一半去装,如果能装满即可以公平分配;如果不能就不不存在公平分配的方案;

根据题意这是一个多重背包,开始是根据01背包写的。。。不管怎么优化都超时。

然后查了下背包问题的资料(http://love-oriented.com/pack/P03.html),顿悟。。。 对于每种物品,每次去1,2,4,8,16.。。。。。件物品。。

code如下:

# include <stdio.h>
# include <stdlib.h>
# include <string>
# include <string.h>

int s[7];

int check() {
	int sum = 0;
	for (int i = 1; i <= 6; ++ i) {
		sum += s[i] * i;
	}
	if (sum % 2) return 0;
	int dp[(sum >> 1) + 1];
	memset (dp, 0, sizeof(dp));
	for (int i = 1; i <= 6; ++ i) {
		for (int j = 0; j <= s[i]; ++ j) {
			int cnt = (1 << j) - 1;
			if (cnt > s[i]) {
				cnt = s[i] - (1 << (j - 1));
				j = s[i] + 1;
			}
			for (int k = sum >> 1; k > 0; -- k) {
				if (i * cnt > k)break;
				if (k - i * cnt >= 0 && dp[k - i * cnt] + i * cnt <= k) {
					dp[k] = dp[k] > dp[k - i * cnt] + i * cnt ? dp[k] : dp[k - i * cnt] + i * cnt;
				}
			}
		}
	}
	//printf ("the sum is %d\n", dp[sum >> 1]);
	if (dp[sum >> 1] == sum >> 1) return 1;
	return 0;
}


int main () {
	int cnt = 0;
	while ((scanf ("%d %d %d %d %d %d", &s[1], &s[2], &s[3], &s[4], &s[5], &s[6]), s[1] + s[2] + s[3] + s[4] + s[5] + s[6])) {
		++ cnt;
		printf ("Collection #%d:\n", cnt);
		if (check()) {
			printf ("Can be divided.\n\n");
		}
		else {
			printf ("Can't be divided.\n\n");
		}
	}
	return 0;
}


你可能感兴趣的:(优化,include)