您的位置:首页 > 其它

poj 1011 Sticks

2015-05-05 08:32 316 查看
Sticks

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 126238Accepted: 29477
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
uva上面提交超时,poj上可以过;
AC代码:


#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 1e4+10;
int len
,sum,L,T;
int used
;
//bool cmp(int x,int y)
//{
//    if(x>y) return true;
//}
int cmp(const void *a,const void *b)
{
return *(int *)b-*(int *)a;
}
bool DFS(int m,int left)//m为剩余的木棒数,left为当前正在拼接的木棒和假定的木棒长度L还缺少的长度
{
if(m == 0 && left == 0)
return true;
if(left == 0)//一根刚刚拼完
left = L;
for(int i=0; i<T; i++)
{
if(!used[i] && len[i]<=left)
{
if(i>0)//如果前者已经出现过的不能用,则当前的也不能用
{
if(!used[i-1] && len[i] == len[i-1])
continue;
}
used[i] = 1;
if(DFS(m-1,left-len[i]))
return true;
else
{
used[i] = 0;
if(len[i] == left || left == L)
return false;
}
}
}
return false;
}
int main()
{
while(scanf("%d",&T) && T)
{   sum  = 0 ;
for(int i=0;i<T;i++)
{
scanf("%d",&len[i]);
sum = sum + len[i];
}
//sort(len,len+T,cmp);          sort  超时
qsort(len,T,sizeof(int),cmp);       //从大到小排序
for(L = len[0];L<=sum/2;L++)
{
if(sum%L) continue;
memset(used,0,sizeof(used));
if(DFS(T,L))
{
printf("%d\n",L);
break;
}
}
if(L>sum/2) printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: