您的位置:首页 > 其它

pku1011

2010-08-13 17:42 197 查看
题意:自己看,麻烦翻译了,这道题是很经典的搜索题,用深搜。。

Sticks

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 72164Accepted: 15847
Description

George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were originally. Please help him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units are integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there are at most 64 sticks. The second line contains the lengths of those parts separated by the space. The last line of the file contains zero.
Output
The output should contains the smallest possible length of original sticks, one per line.
Sample Input
9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output
6
5


<code>

#include<iostream>
#include<algorithm>
using namespace std;
const int size=65;
int a[size],used[size];
int len; //原始长度
int m;//原始棍子根数
int flag;
int n;
int cmp(int a,int b)
{
return a>b;
}
void dfs(int k,int sum,int cnt)
{

if(cnt==m)
flag=true;
else if(sum==len)
dfs(0,0,cnt+1);
else
{
for(int pre=-1,i=k;i<n;i++)
{
if(!used[i]&&a[i]!=pre&&sum+a[i]<=len)
{
pre=a[i];
used[i]=true;
dfs(i+1,a[i]+sum,cnt);
used[i]=false;
if(k==0||flag)
return;
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
int i,sum=0;
for(i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
}
sort(a,a+n,cmp);//从大到小排序
flag=0;
for(i=a[0];i<=sum/2;i++)
{
if(sum%i==0)
{
len=i;
m=sum/i;
memset(used,0,sizeof(used));
dfs(0,0,0);
if(flag)
break;
}
}
printf("%d/n",flag?len:sum);//sum全部接到一根棍子上去
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: