pku1011
2010-08-13 17:42
197 查看
题意:自己看,麻烦翻译了,这道题是很经典的搜索题,用深搜。。
Sticks
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
Sample Output
<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;
}
Sticks
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 72164 | Accepted: 15847 |
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;
}
相关文章推荐
- pku 1011 sticks
- pku 1011 sticks 经典DFS+剪枝
- pku 1011 sticks 搜索+剪枝 解题报告
- PKU 1011
- PKU1011解题ing
- pku 1011 解题报告
- pku-1011-Sticks
- pku1011
- pku 1011(dfs)
- PKU-1011 Sticks (DFS + 剪枝)
- pku1011 深搜+强剪枝
- PKU1011 Sticks
- PKU 1011
- pku 1011 sticks
- PKU 1011 Sticks
- pku 1011 解题报告
- PKU 1011 Sticks
- ACM PKU 1011 Sticks 深度优先搜索
- PKU 1011 Sticks
- 北大PKU在线测试1011