Sticks
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 119772 Accepted: 27683
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
Source
Central Europe 1995
求若干根长短不一的小木棒,能组成长度一样的若干大棍的最小长度
思路:深度优先搜索DFS+剪枝
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 119772 Accepted: 27683
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
Source
Central Europe 1995
求若干根长短不一的小木棒,能组成长度一样的若干大棍的最小长度
思路:深度优先搜索DFS+剪枝
AC代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
int Length[65];
int Used[65];//记录木棒是否被用过
int TotalLength,L,n,nLast;
int cmp(int x,int y)
{
if(x!=y) return x>y;
}
int DFS(int R,int M)//R是能用的木棒数
{
//M表示当前正在拼的棍子和 L 比还缺的长度
if(R==0&&M==0)
return 1;
if(M==0)//表示一根已经拼完,要拼另外一根
M=L;
int x=0;
if(M!=L)
x=nLast+1;//剪枝4,从刚刚(最近)接上去的那条木棒的下一条开始找
for(int i=x;i<n;i++)
{
if(!Used[i]&&Length[i]<=M)
{
if(i>0)
{
//剪枝1,如果相同长度的木棒原来没有用过,则不需要再用
if(Used[i-1]==0&&Length[i]==Length[i-1])
continue;
}
Used[i]=1;nLast=i;
if(DFS(R-1,M-Length[i]))
return 1;
else
{
Used[i]=0;//说明本次不能用第i根
//第i根以后还有用
if(Length[i]==M||M==L)//剪枝2,如果连第一根都没有用到的话,
//直接就拼不成这种长度(若能拼成,则第一根一定能在其他的棒中组成)
return 0;
}
}
}
return 0;
}
int main()
{
int i,j,m;
while(scanf("%d",&n),n!=0)
{
memset(Length,0,sizeof(Length));
TotalLength=0;
for(i=0;i<n;i++)
{
scanf("%d",&Length[i]);
Used[i]=0;
TotalLength+=Length[i];//计算木棒的总长度
}
sort(Length,Length+n,cmp);//将木棒从长到短排序
for(L=Length[0];L<=TotalLength/2;L++)
{
if(TotalLength%L)
continue;
memset(Used,0,sizeof(Used));
if(DFS(n,L))
{
printf("%d\n",L);
break;
}
}
if(L>TotalLength/2)//最坏的情况,最中只能拼得所有木棒的总长度
printf("%d\n",TotalLength);
}
return 0;
}