UESTC Training for Search Algorithm——L
2012-05-20 01:33
387 查看
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 helphim 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
95 2 1 5 2 1 5 2 1
4
1 2 3 4
0
Sample Output
65
Source
Central Europe 1995/*算法思想: 我用DFS做的,先将所有的木棍从大到小排序,然后从最大的木棍开始枚举木棍能 拼接的长度len,要是能拼接,退出循环,输出结果 剪枝1:要是这跟木棍不能用来拼接,那么其他的和他长度相等的木棍也不能拼接 剪枝2:不能找到一根长度为len的木棍,直接退出搜索,以后也不能找到了 */ /*CODE*/ #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<algorithm> #define N 100 using namespace std; int a ,sum,n,len; bool fg[80]; bool cmp(int a,int b) { return a>b; } bool dfs(int v,int left,int tot) //DFS,当前用到第v根木棍了,还剩下len凑够一根长度为len的木棍,总的木棍长度还剩tot { if(tot==len) return true; //如果总的木棍长度还剩tot,一定能够凑够 for(int i=v;i<n;i++) if(!fg[i] && a[i]<=left) //找一根能够用来拼凑的木棍 { fg[i]=true; if(a[i]==left) { if(dfs(0,len,tot-a[i])) return true; //找新的一根长度为len的木棍 } else if(dfs(i+1,left-a[i],tot-a[i])) return true; fg[i]=false; if(a[i]==left) return false; //不能找到一根新的长度为len的木棍,肯定不能找到了,返回false if(tot==sum) return false; //用于第一次DFS,要是不能找到,返回false if(left==len) return false; //不能找到一根长度为len的木棍,返回false while(a[i]==a[i+1]) i++; //相同的木棍,上一个满足条件找了不能找到,这一个也一样的不能 } return false; } int main() { while(scanf("%d",&n),n!=0) { sum=0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } sort(a,a+n,cmp); memset(fg,0,sizeof(fg)); for(len=a[0];len<=sum;len++) { if(sum%len!=0) continue; if(dfs(0,len,sum)) { printf("%d\n",len); break; } } } return 0; }
相关文章推荐
- 2016 UESTC Training for Search Algorithm & String H - 中二少女与字符串 Trie 字典树
- 2014 UESTC Training for Search Algorithm Problem F 方老师与迷宫
- UESTC Training for Search Algorithm——B
- UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>
- 2016 UESTC Training for Search Algorithm & String I - 谭爷剪花布条 KMP
- UESTC Training for Search Algorithm——C
- UESTC_王之迷宫 2015 UESTC Training for Search Algorithm & String<Problem A>
- UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
- 2016 UESTC Training for Search Algorithm & String K - 卿大爷的三个女友 KMP、跳转数组
- 2016 UESTC Training for Search Algorithm & String(A B C E I K)
- UESTC Training for Search Algorithm——D
- UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
- UESTC Training for Search Algorithm——E
- UESTC Training for Search Algorithm——F
- UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>
- 2015 UESTC Training for Search Algorithm & String - J - 全都是秋实大哥 【KMP】
- UESTC Training for Search Algorithm——G
- UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>
- 2014 UESTC Training for Search Algorithm B
- UESTC Training for Search Algorithm——H