• POJ1011 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 }
    功不成,身已退
  • 相关阅读:
    软件工程,实践作业1_团队博客
    软件工程,实践作业1
    c# excel 读写 64位操作系统 64位excel
    pyfits fits图像区域选择
    python numpy中sum()时出现负值
    python 中模块的版本号
    numpy rand函数的应用
    python 字符串是否包含某个子字符串
    python 字符串格式化
    python 让异常名称显示出来
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2638926.html
Copyright © 2020-2023  润新知