题意简述
给出n个数,求最小的l,使n个数可分成若干组,每组和都为l。
题解思路
暴力搜索+剪枝
代码
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int n, sum, minn, maxx;
int t[70];
void dfs(int gs, int last, int sy, int qb)
{
if (gs == 0)
{
printf("%d
", qb);
exit(0);
}
if (sy == 0)
dfs(gs - 1, maxx, qb, qb);
bool flag = true;
for (int i = min(last, sy); i >= 1; --i)
if (t[i] > 0)
{
flag = false;
t[i] --;
dfs(gs, i, sy - i, qb);
t[i] ++;
if (i == sy || sy == qb)
return;
}
if (flag)
return;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
int x;
scanf("%d", &x);
if (x <= 50)
{
t[x] ++;
sum += x;
maxx = max(maxx, x);
}
}
for (int i = maxx; i <= sum; ++i)
if (sum % i == 0)
dfs(sum / i, maxx, i, i);
}