Sticks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11007 Accepted Submission(s): 3342
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
6 5
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn = 70;
int n,a[maxn],used[maxn],total,length,stick_num;
bool dfs(int num,int pos,int res){ //和HDOJ 1518深搜写法十分类似
if(num==stick_num) return true;
for(int i=pos;i<=n;i++){
if(used[i]) continue;
used[i] = true;
if(a[i]==res){
if(dfs(num+1,0,length)) return true;
}
else if(a[i]<res){
if(dfs(num,i+1,res-a[i])) return true;
}
used[i] = false;
if (res==length) return false; //没有找到合适的解,剪枝
while (i+1<=n && a[i+1]==a[i]) i++; //如果二者相同,因为之前的深搜已经得出结论这条线走不通,所以直接排除掉再选一次相同数字的可能
}
return false;
}
int main(){
while (cin>>n && n){
total=0;
for (int i=1;i<=n;i++) {
cin>>a[i];
total+=a[i];
}
sort(a+1,a+n+1,greater<int>()); //从大到小排序
memset(used,0,sizeof(used));
for (int i=1;i<=total;i++) if (total%i==0){
stick_num=total/i;
length=i;
if (dfs(0,1,length)) {
printf("%d\n",i);
break;
}
}
}
return 0;
}
455



被折叠的 条评论
为什么被折叠?



