Sticks
Time Limit:1000MS Memory Limit:10000K
Total Submit:24457 Accepted:5494
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
对于准备学习回溯剪枝的人来说,这道题是再适合不过了 :)
s[]记录数据,将数据按递减顺序排序,搜索数d,len = sum/d
目标:搜索len的组合
解题思路:
1. res+s[i] == d
搜索成功返回1,否则返回0
2. res+s[i] < d
选取s[i]结点, 继续从下一点开始搜索,成功则返回1;
如果失败,判断本次搜索起始是否是一个搜索起始段,即res=0,如果是,手机APP ,则
返回0。因为此时s[i]必为从最左端开始搜索,第一个还没有被用的点,故而如果
有解,则必然包含s[i],所以,本次搜索如果失败,在res=0的情况下,网站SEO,返回0
减少不必要的搜索。