poj 1011 搜索减枝

题目链接:http://poj.org/problem?id=1011

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = ; int a[maxn];
int N;
int sum;
int len,cnt;
bool used[maxn]; bool cmp(int a,int b){
return a > b;
}
bool dfs(int u,int curlen,int num){
if(num == cnt) return true; for(int i=u;i<=N;i++){
if(used[i]) continue;
if(!used[i-] && a[i-] == a[i]) continue; if(curlen + a[i] == len){
used[i] = true;
if(dfs(num+,,num+)) return true;
used[i] = false;
return false;
}
else if(curlen + a[i] < len){
used[i] = true;
if(dfs(i+,curlen+a[i],num)) return true;
used[i] = false;
if(curlen == ) return false; //一个大的都不能够找到答案,一个小的就更不可能了。
}
}
return false;
}
int main()
{
// freopen("E:\\acm\\input.txt","r",stdin);
while(cin>>N && N){
sum = ;
for(int i=;i<=N;i++){
scanf("%d",&a[i]);
sum += a[i];
}
sort(a+,a+N+,cmp);
a[] = -;
for(len=a[];len<sum;len++){
if(sum % len) continue;
cnt = sum / len;
memset(used,,sizeof(used)); if(dfs(,,)) break;
}
printf("%d\n",len);
}
}
上一篇:VIM中使用S查找并替换


下一篇:Heron and His Triangle HDU - 6222(pell 大数)