uva 307

排序之后再剪枝,有点神

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 200010
#define INF 0x7fffffff
#define inf 10000000
#define ull unsigned long long
#define ll long long
using namespace std; int a[65], n;
bool vis[65];
bool cmp(int a, int b)
{
return a > b;
}
bool dfs(int cc, int now, int aim, int k, int cur)
{
// printf("*****%d***%d***\n", now, cur);
if(cur == cc) return true;
for(int i = k; i < n; ++ i)
{
//if(i && !vis[i-1] && a[i] == a[i-1]) continue;
if(!vis[i] && a[i] < now)
{
vis[i] = 1;
if(dfs(cc, now-a[i], aim, i+1, cur)) return true;
vis[i] = 0;
if(now == aim) //第一个棒如果用不到,显然不能成功,直接结束
return false;
}
else if(!vis[i] && a[i] == now)
{
vis[i] = 1;
if(dfs(cc, aim, aim, 0, cur+1)) return true;
vis[i] = 0;
return false; //因为棒从大到小排,如果无法成功,直接结束
}
}
return false;
}
int main()
{
while(scanf("%d", &n) == 1 && n)
{
int _max = 0, sum = 0, ans;
memset(vis, 0, sizeof(vis));
for(int i = 0; i < n; ++ i)
{
scanf("%d", &a[i]);
_max = max(_max, a[i]);
sum += a[i];
}
sort(a, a+n, cmp);
for(int i = _max; i <= sum; ++ i)
{
if(sum%i == 0 && dfs(sum/i - 1, i, i, 0, 0))
{
ans = i;
break;
}
}
printf("%d\n", ans);
}
return 0;
}
上一篇:如何在IOS开发中在自己的framework中添加.bunble文件


下一篇:紫书 习题7-14 UVa 307(暴搜+剪枝)