记录编号 388593 评测结果 AAAAAAAAAA
题目名称 [POJ 1011] 木棍拼接 最终得分 100
用户昵称 GravatarkZime 是否通过 通过
代码语言 C++ 运行时间 0.004 s
提交时间 2017-03-29 14:00:31 内存使用 0.31 MiB
显示代码纯文本
/*kZime*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <algorithm>
#define MAXN
using namespace std;
inline int read() {
	int k = 0, f = 1; char c = getchar();
	for(; !isdigit(c); c = getchar())if(c == '-') f = -1;
	for(; isdigit(c); c = getchar()) k = k * 10 + c - '0';
	return k * f;
}
/*-----------------------------------------------------------------------------*/

int n, ans, st[65], sum;
bool vis[65];
bool cmp(const int &a, const int &b) {
	return a > b;
}

bool dfs(int now, int beg, int cnt) {
	if(ans * cnt == sum) return true;
	for(int i = beg; i < n; i++) {
		if(vis[i] || (i && !vis[i - 1] && st[i] == st[i - 1]))continue;

		if(now + st[i] == ans) {
			vis[i] = 1;
			if(dfs(0, 0, cnt + 1)) return true;
			vis[i] = 0;
			return false;
		}	
		else if(now + st[i] < ans) {
			vis[i] = 1;
			if(dfs(now + st[i], i + 1, cnt))return true;
			vis[i] = 0;
			if(!now)return false;
		}
	}
	return false;
}

int main() {
#ifndef MYLAB
	freopen("sticka.in", "r", stdin);
	freopen("sticka.out", "w", stdout);
#else
	freopen("in.txt", "r", stdin);
#endif
		scanf("%d", &n);
		sum = 0;
		for(int i = 0;i < n; i++) {
			st[i] = read();
			if(st[i] > 50) st[i] = 0;
			sum += st[i];
		}
		sort(st, st + n, cmp);
		for(ans = st[0]; ans < sum; ans++) {
			if(sum % ans)continue;
			memset(vis, 0, sizeof(vis));
			if(dfs(0, 0, 0)) {
				break;
			}
		}
		printf("%d\n", ans);
		return 0;
}