Sticks POJ

来源:互联网 发布:按键精灵源码库 编辑:程序博客网 时间:2024/06/05 04:45

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
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65


很经典的dfs+剪枝

感觉自己对剪枝理解的不是很透彻,很多剪枝方面问题想不到,以后要注意

从基础学,研究如果不是用编程实现,拿到题目自己应该如何得出答案,如何能够更简单,如何变得通用


具体说一下这个题目:

第一:输入遍历完毕后,将其逆序排列,从最大的开始试验,以第一个实例为例,排列后5 5 5 2 2 2 1 1 1

从5到最大值(总和)试验,对于5来说,首先5就直接合适,3个5试验完毕后,测试2,最后发现不是很合适

对于6来说,首先将5(最大的一个数)放进去,发现缺少,往后遍历只有1合适了,放进去,发现正好等于6,即完成一个试验、、、、

以此类推


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int n;int a[65];int book[65];int flag,len;int cmp(int a,int b){return a>b;}void dfs(int k,int now_len,int p){if(flag)//若找到合适的木棒长度后可以直接return{return ;}if(now_len==0)//每当完成一个木棒的拼接后,或者是每拼接一个新的木棒时,首先将存在的最长的木棒放入新的要做的木棒拼接{k=0;while(book[k])//寻找最长的那个短木棒{k++;}book[k]=1;dfs(k+1,now_len+a[k],p+1);//最长的都找不到匹配的话returnbook[k]=0;return ;}if(now_len==len)//当完成一个木棒时,可能是全部完成的情况{if(p==n){flag=1;return ;}else{dfs(0,0,p);}}for(int i=k;i<n;i++){if(!book[i]&&now_len+a[i]<=len){if(!book[i-1]&&a[i]==a[i-1])//剪枝,重复的直接去掉{continue;}book[i]=1;dfs(k+1,now_len+a[i],p+1);book[i]=0;}}}int main(){while(~scanf("%d",&n)){if(n==0){break;}int sum=0;for(int i=0;i<n;i++){scanf("%d",&a[i]);sum+=a[i];}sort(a,a+n,cmp);for(len=a[0];len<sum;len++){if(sum%len==0){memset(book,0,sizeof(book));flag=0;dfs(0,0,0);if(flag){break;}}}printf("%d\n",len);}return 0;}



0 0
原创粉丝点击