社区讨论

42pts能想到的和看到的题解优化都用光了,求指点

P1120[CERC 1995] 小木棍参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@lo35unuw
此快照首次捕获于
2023/10/24 01:17
2 年前
此快照最后确认于
2023/10/24 01:17
2 年前
查看原帖
CPP
#include <cstdio>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>

const int N = 66;
using namespace std;
short n, a[N], node[N], sum, maxn, tag;
inline void dfs(short len, short k, short r) {
	//原长为len棍子第k根还有r没拼
	if (k == tag&&r==0) {
		printf("%d", len);
		exit(0);
	}
	if (r == 0) {
		dfs(len, k + 1, len);
		return ;
	}
	short key = (lower_bound(a + 1, a + 1 + a[0], r,greater<short>()) - a) ;//cut4
	while (key<=a[0]) {
		if(node[a[key]]){
			node[a[key]]--;
			dfs(len,k,r-a[key]);
			node[a[key]]++;
			if(r-a[key]==0)return;//cut5
		}
		key++;
	}
	return ;
}
inline bool cm_p(short a, short b) {
	return a > b;
}

int main() {
	scanf("%d", &n);
	short x;
	for (short i = 1; i <= n; i++) {
		scanf("%d", &x);
		if (x > 50)continue;
		sum += x;
		maxn = max(maxn, x);
		if (!node[x])a[++a[0]] = x;
		node[x]++;//cut4
	}
	sort(a + 1, a + 1 + a[0], cm_p); //cut1
	for (short i = maxn; i <= sum / 2; i++) {//cut3
		if (sum % i != 0)continue; //cut2
		tag = sum / i;
		dfs(i, 1, i);
	}
	printf("%d", sum);
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...