poj1011 _经典搜索

来源:互联网 发布:微信支付退款接口 php 编辑:程序博客网 时间:2024/06/06 05:48
Sticks
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 133220 Accepted: 31285

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

95 2 1 5 2 1 5 2 141 2 3 40

Sample Output

65

/**************************************

经典搜索题,剪枝众多。。。上代码

**************************************/

</pre><div><pre name="code" class="cpp">#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <cmath>#include <vector>#include <bitset>using namespace std;#define INT_MAX 1 << 30typedef long long ll;int n;int sum;int a[100];int used [100];int l;int cmp(const void *a,const void *b)            {        return *(int *)b - *(int *)a;}int dfs(int num,int length)                        //剩下的木棍数和拼接需要的棍子长度{    if(num == 0 && length == 0)     return 1;    //搜索结束    if(length == 0)        length = l;                //搜索的继续    for (int i = 0; i < n; i += 1)    {        if(!used[i]&&(a[i] <= length))          //木棒的长度必须小于需要的长度        {            if (i > 0)                          //排序后相等的相邻,如果i失败了相等的i-1跳过,剪枝4            {                if(!used[i-1]&&(a[i] == a[i-1]))                {                    continue;                }            }            used[i] = 1;            if(dfs(num-1,length-a[i]))                return 1;                else             {                used[i] = 0;                    //搜索失败回溯,深搜的鲜明特征                if(length == a[i])  return 0;   //棍子的最后一根短棒失败,不必找更短棒代替                if(length == l)     return 0;   //棍子的第一根长棒失败,后面短棒不必再循环            }        }    }    return 0;}int main(){    while ((scanf("%d",&n) != EOF) && (n != 0))    {        sum = 0;        memset(a,0,sizeof(a));              //初始化        for (int i = 0; i < n; i += 1)        {            cin >> a[i];            sum += a[i];        }        qsort(a,n,sizeof(a[0]),cmp);      //从大到小进行排序,提高搜索效率,剪枝1        for (l = a[0]; l <= sum/2; l += 1)//棍子的最短长度不能低于木棒的最长长度,剪枝2        {            if (sum%l)                      //非因子直接跳过,剪枝3                {                    continue;                }            memset(used,0,sizeof(used));  //每次搜索都要初始化                        if (dfs(n,l))                  //由于搜索最短木棒,成功后直接跳出            {                cout << l << endl;                break;            }        }        if(l > sum/2)            cout << sum << endl;            }    return 0;}
欢迎指错与讨论

1 0