POJ-1011 Sticks


Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 99807   Accepted: 22696

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
  1 /* 
  2    功能Function Description:    POJ-1011 
  3    開發環境Environment:         DEV C++ 4.9.9.1
  4    技術特點Technique:
  5    版本Version:
  6    作者Author:                  可笑痴狂
  7    日期Date:                    20120814
  8    備注Notes:                   ----經典深搜題(主要在剪枝)
  9    解題思路:  
 10             思想很簡單,一個接一個的把木棍拼起來,最后把木棍用光。
 11             關鍵的地方是幾個剪枝技巧:
 12                    設所有木棍的總長度為 Sum, 最終的答案(長度)是 L。 
 13              1. 首先要明白, Sum一定要能被 L 整除。 
 14              2. L 一定 大於等於 題目給出的最長的木棍的長度 Max。
 15                   由上述兩點,我們想到,可以從 Max 開始遞增地枚舉 L, 
 16                 直到成功地拼出 Sum/L 支長度為 L 的木棍。
 17              搜索中的剪枝技巧: 
 18              3. 將輸入的輸入從大到小排序,這么做是因為一支長度為 K 
 19                 的完整木棍,總比幾支短的小木棍拼成的要好。
 20                 形象一些:
 21                   如果我要拼 2 支長為8的木棍,第一支木棍我拼成 
 22                           5 + 3
 23                   然后拼第二支木棍但是失敗了,而我手中還有長為 2 和 1 
 24                   的木棍,我可以用 5 + 2 + 1 拼好第一支,再嘗試拼第二
 25                   支,仔細想一想,就會發現這樣做沒意義,注定要失敗的。     
 26                   我們應該留下 2+1 因為 2+1 比 3 更靈活。 
 27              4. 相同長度的木棍不要搜索多次, 比如:
 28                 我手中有一些木棍, 其中有 2 根長為 4 的木棍, 當前搜索
 29                 狀態是 5+4+.... (即表示長度為 5,4,2 的三支拼在一起, 
 30                 ...表示深層的即將搜索的部分), 進行深搜后不成功,故我
 31                 沒必要用另一個 4 在進行 5+4+...
 32              5. 將開始搜索一支長為 L 的木棍時,我們總是以當前最長的未
 33                 被使用的 木棍開始,如果搜索不成功,那么以比它短的開始
 34                 那么也一定不能取得全局的成功。因為每一支題目給出的木棍
 35                 都要被用到。
 36                 如果,有 
 37                     4
 38                     5 4 4 3 2
 39                   想拼成長為 6 的木棍,那么從 5 開始, 但是顯然沒有能與 5
 40                   一起拼成 6 的,那么我就沒必要去嘗試從 4 開始的,因為
 41                   最終 5 一定會被遺棄。在拼第 2 3 ... 支木棍時,一樣。
 42 */
 43 
 44 #include<cstdio>
 45 #include<cstdlib>
 46 #include<cstring>
 47 
 48 int len[65];
 49 bool used[65];
 50 int sum,L,n;
 51 
 52 int cmp(const void *a,const void *b)
 53 {
 54     return *(int *)b-*(int *)a;
 55 }
 56 
 57 bool DFS(int m,int left)    //m為剩余的未用的木棒數,left為當前正在拼接的木棒和假定的木棒長度L比還缺少的長度
 58 {
 59     if(m==0&&left==0)
 60         return true;
 61     if(left==0)     //一根剛剛拼完開始拼新的一根
 62         left=L;
 63     for(int i=0;i<n;++i)
 64     {
 65         if(!used[i]&&len[i]<=left)
 66         {
 67             if(i>0)
 68             {
 69                 if(!used[i-1]&&len[i]==len[i-1])    //第一次剪枝:
 70                     continue;                        //如果當前的沒用過的棒子不可用,那么和他長度相同的未使用的棒子也不可用,直接跳過
 71             }
 72             used[i]=true;
 73             if(DFS(m-1,left-len[i]))
 74                 return true;
 75             else
 76             {
 77                 used[i]=false;//說明不能用i作為第一條,那么要拆以前的木棒,i還可能用在以前的木棒中,所以回溯
 78                 if(len[i]==left||left==L)  //重要剪枝---很重要否則會超時
 79                     return false;          //將開始搜索一支長為 L 的木棍時,我們總是以當前最長的未
 80             }                               //被使用的 木棍開始,如果搜索不成功,那么以比它短的開始
 81         }                                   //也一定不能取得全局的成功。因為每一支題目給出的木棍都要被用到。
 82     }                                       //這里用時16Ms,去掉len[i]==left這個條件(不太懂---網上說當前木棒是最后一根木棒  )變成47Ms,而去掉left--L這個條件會超時
 83     return false;    
 84 }
 85 
 86 int main()
 87 {
 88     while(scanf("%d",&n),n)
 89     {
 90         sum=0;
 91         for(int i=0;i<n;++i)
 92         {
 93             scanf("%d",&len[i]);
 94             sum+=len[i];
 95         }
 96         qsort(len,n,sizeof(int),cmp);       //從大到小排序
 97         for(L=len[0];L<=sum/2;++L)          //若L>sum/2則只有一種可能就是所有木棒只能拼接成一根。
 98         {
 99             if(sum%L)
100                 continue;
101             memset(used,false,sizeof(used));
102             if(DFS(n,L))
103             {
104                 printf("%d\n",L);
105                     break;
106             }
107         }
108         if(L>sum/2)
109             printf("%d\n",sum);
110     }
111     return 0;
112 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM