• Sticks(poj 1011)


    题目描述:

    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 Output6

    5

    代码如下:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdlib>
     4 
     5 using namespace std;
     6 
     7 int stick[65],visit[65];
     8 int n,len,flag,sum;
     9 
    10 int cmp(const void * a,const void * b)
    11 {
    12     return (*(int *)a - *(int *)b);
    13 }
    14 
    15 void dfs(int rest,int complete,int start);
    16 int main()
    17 {
    18     while(cin >> n && n)
    19     {
    20         sum = 0;
    21         flag = false;
    22         for(int i = 0;i < n;i++)
    23         {
    24             cin >> stick[i];
    25             sum += stick[i];
    26         }
    27         qsort(stick,n,sizeof(int),cmp);//从长到短的排序,先排短木棒的话,很
    28         memset(visit,0,sizeof(visit));//有可能出现后面一根木棒都不能填补的情况
    29         for(len = stick[0];len <= sum/2;len++)//枚举区间应该在sum/2,因为最少
    30         {                            //组成两根木棒
    31             if(sum % len == 0)//拼好的木帮要整除木棒和,这样才能平均分
    32             {
    33                 dfs(len,0,0);
    34                 if(flag)//出现符合情况,就退出循环
    35                     break;
    36             }
    37         }
    38         if(flag)
    39             cout << len << endl;
    40         else
    41             cout << sum << endl;
    42     }
    43     return 0;
    44 }
    45 
    46 void dfs(int rest,int complete,int start)//rest为拼当前木棒时还缺少的长度
    47 {//complete为已经拼好的木棒数,start为从哪根木棒开始搜索待拼的木棒
    48     if(flag)
    49         return;
    50     if(rest == 0)
    51     {
    52         complete++;
    53         if(complete == (sum / len))
    54             flag = 1;
    55         else
    56         {
    57             int k = 0;
    58             while(visit[k])
    59                 k++;
    60             visit[k] = 1;
    61             dfs(len - stick[k],complete,k+1);
    62             visit[k] = 0;//回溯
    63         }
    64     }
    65     else
    66     {
    67         for(int i = start;i < n;i++)
    68         {
    69             if(i > 0 && !visit[i - 1] && (stick[i - 1] == stick[i]))
    70                 continue;//如果当前木棒和前一根木棒是一样的长度,而前一根木棒没有用过的话,那么这根木棒也一定不使用的
    71             if(!visit[i] && stick[i] <= rest)
    72             {
    73                 visit[i] = 1;
    74                 dfs(rest - stick[i],complete,i + 1);
    75                 visit[i] = 0;
    76                 if(rest == stick[i])//注释1 && 注释2(注释1 和 注释2 是同一种解释)
    77                     break;
    78             }
    79         }
    80     }
    81     return;
    82 }
    83 
    84 /*注释1*/
    85 /*搜索过程中,如果这个时候rest==d,也就是说刚刚开始拼一根新的木棒,
    86  * 假如这时把没有用过的最长的一根木棒拼上去,但最后无解的话,
    87  * 那么便不需要去尝试把后面的木棒拼上去了,
    88  * 因为后面不管第几次拼,总要用到这根木棒的,
    89  * 所以出现这种情况,不管后面再尝试拼哪根,最后都一定是无解的。*/
    90 
    91 /*注释2*/
    92  /* 搜索过程中,如果遇到一根木棒,它的长度恰好等于rest,
    93  * 但把它拼上去之后无解,那么也便不需要再尝试把后面的木棒拼上去了*/

    代码分析:

    剪枝非常重要,之前超时了好多次。。。

    参考地址:http://www.cnblogs.com/staginner/archive/2011/08/17/2143614.html

  • 相关阅读:
    线程的实现方式
    实现一个拷贝文件的工具类,要使用字符流还是字节流
    String&&StringBuilder&&StringBuffer
    面向对象的特征
    索引的选择
    TCP之Nagle算法&&延迟ACK
    通用套接字选项和TCP套接字选项
    TCP之非阻塞connect和accept
    TCP之种种连接异常
    TCP之listen&backlog
  • 原文地址:https://www.cnblogs.com/linxiaotao/p/3468312.html
Copyright © 2020-2023  润新知