POJ1011 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

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

Source

中文:

Description

乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位。然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度。请你设计一个程序,帮助乔治计算木棒的可能最小长度。每一节木棍的长度都用大于零的整数表示。

Input

输入包含多组数据,每组数据包括两行。第一行是一个不超过64的整数,表示砍断之后共有多少节木棍。第二行是截断以后,所得到的各节木棍的长度。在最后一组数据之后,是一个零。

Output

为每组数据,分别输出原始木棒的可能最小长度,每组数据占一行。

Sample Input

9
5 2 1 5 2 1 5 2 1
4
1 2 3 4
0

Sample Output

6
5

Source

Translator

北京大学程序设计实习, Xie Di
 
 

正解:搜索+各种强力剪枝

解题报告:

  明天就要去北京参加夏令营了,今晚练一练手感。
  一直知道这道经典搜索题,然而一直没有打。今天打了一下,高兴地一遍过样例,然后交一发高兴地TLE。于是自己yy出了几个剪枝,调了一下,感觉快了很多,然而交了之后还是TLE,

  无奈上网蒯剪枝,发现如果当前需要搜一根新棒并且当前最长的那根用进去不可以那么可以直接return,这个强力剪枝让我直接AC了。

  调了1个多小时。。。

  其他的剪枝还是比较好想到的吧。

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #ifdef WIN32   
13 #define OT "%I64d"
14 #else
15 #define OT "%lld"
16 #endif
17 using namespace std;
18 typedef long long LL;
19 const int MAXN = 71;
20 int n;
21 int a[MAXN];
22 int ans,total;
23 int ljh;
24 bool ok;
25 bool use[MAXN];
26 
27 inline int getint()
28 {
29        int w=0,q=0;
30        char c=getchar();
31        while((c<'0' || c>'9') && c!='-') c=getchar();
32        if (c=='-')  q=1, c=getchar();
33        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
34        return q ? -w : w;
35 }
36 
37 inline bool cmp(int q,int qq){ return q>qq; }
38 
39 inline void Init(){
40     total=0;
41     for(int i=1;i<=n;i++) a[i]=getint(),total+=a[i];
42     sort(a+1,a+n+1,cmp);
43     ans=a[1]; ok=false;
44     memset(use,0,sizeof(use));
45 }
46 
47 inline void dfs(int now,int len,int zong){
48     if(zong==n) {  ok=1; return ;  }
49     int cun=-1;//上一个不可行的值
50     for(int i=now;i<=n;i++) {
51     if(use[i] || cun==a[i]) continue; 
52     use[i]=1;
53     if(a[i]+len<ans){
54         dfs(i,a[i]+len,zong+1);
55         if(ok) return ;
56         else cun=a[i];
57     }
58     else if(a[i]+len==ans){
59         dfs(0,0,zong+1);//从0开始
60         if(ok) return ;
61         else cun=a[i];
62     } 
63     use[i]=0;
64     if(len==0) break;//构建新棒时,对于新棒的第一根棒子,在搜索完所有棒子后都无法组合,则说明该棒子无法在当前组合方式下组合,不用往下搜索(往下搜索会令该棒子被舍弃),直接返回上一层
65     }
66 }
67 
68 inline void solve(){
69     while(scanf("%d",&n)!=EOF) {
70     if(n==0) break;
71     Init();
72     for(;ans<=total-ans;ans++) //只需判断到一半
73         if(total%ans==0)  {
74         dfs(1,0,0); 
75         if(ok) break; 
76         }
77     if(ok)
78         printf("%d
",ans);
79     else printf("%d
",total);
80     }
81 }
82 
83 int main()
84 {
85   solve();
86   return 0;
87 }
原文地址:https://www.cnblogs.com/ljh2000-jump/p/5554525.html