牛客网 poj 1011
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 0Sample Output
6 5一组等长木棍,砍成n份,现将n份重新组合,问原木棍的长度
dfs+剪枝 思路就是枚举每一种长度的可能,然后去验证是否成立 如果直接这样做肯定超时,我们需要优化,也就是剪枝 1.将原木棍的长度从小到大排列,并按照这个顺序进行搜索,原木棍的长度肯定在1~所有木棍之和范围内,也大于等于现所有木棍 2.如果我们探究第x个木块是否能匹配,如果与第i个木棍不能拼成假设长度,则和第i个木棍一样长的都不可以,这样可以跳过一批 3。如果对于一个木棍,找不到任何一个或多个木棍拼成预设原木棍长度,那说明该预设原木棍长度不存在,枚举下一个 对应代码部分就是
if(ret==a[i] || len==ret)break;我是这么理解的: ret = = a[i]表示可以与a[i],但是之后的木棍无法拼上,依旧不符合题意 len = = ret说明之后的任何一个木棍都拼不上
qosrt和sort我一直认为是qsort快,但也有说sort快的,我也是一脸懵逼
int num[maxn]; int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; } qsort(num,maxn,sizeof(int),cmp);