[UVA307]小木棍 Sticks

题目大意:有一堆小木棍,把它们接成相同长度的小木棍,问结果的小木棍的最小长度是多少,多组数据

题解:$dfs$,各种剪枝。

卡点:



C++ Code:

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#define maxn 55
const int inf = 0x3f3f3f3f;

int n, Min, Max, sum;
int cnt[maxn];
bool halt;
void dfs(int res, int sum, int len, int now) {
	if (!res) {
		printf("%d\n", len);
		halt = true;
		return ;
	}
	if (sum == len) return dfs(res - 1, 0, len, Max);
	for (int i = now; i >= Min; --i) if (cnt[i] && sum + i <= len) {
		--cnt[i];
		dfs(res, sum + i, len, i);
		if (halt) return ;
		++cnt[i];
		if (!sum || sum + i == len) return ;
	}
}

int main() {
	scanf("%d", &n);
	while (n) {
		Min = inf, Max = sum = 0, halt = false;
		__builtin_memset(cnt, 0, sizeof cnt);
		for (int i = 0, x; i < n; ++i) {
			scanf("%d", &x);
			if (x <= 50) {
				++cnt[x], sum += x;
				Min = std::min(x, Min);
				Max = std::max(x, Max);
			}
		}
		for (int i = Max; i <= sum >> 1; ++i) if (sum % i == 0) dfs(sum / i, 0, i, Max);
		if (!halt) printf("%d\n", sum);
		scanf("%d", &n);
	}
	return 0;
}

 

转载于:https://www.cnblogs.com/Memory-of-winter/p/10326407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值