zoukankan      html  css  js  c++  java
  • [POJ] 1011 Sticks

    Sticks
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 115683   Accepted: 26614

    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

    Source

     
     
    题解:深度优先搜索经典题目。需要配合各种剪枝。
            1.把木棍长度从大到小排序,从长的开始搜极大减少搜索次数。
        2.长度a[i]的不能匹配,因为有序,和它等长的全部剪掉。
     
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdbool.h>
     4 int i,j,n,m,maxi,sum,ans,
     5     a[100];
     6 
     7 bool vis[100];
     8 
     9 int 
    10 pre()
    11 {
    12     sum=0;ans=0;
    13     memset(a,0,sizeof(a));
    14     memset(vis,false,sizeof(vis));
    15     
    16     return 0;
    17 }
    18 
    19 bool 
    20 dfs(int num,int pos,int len)
    21 {
    22     int i;
    23     if (num==n) return true;
    24     for(i=pos;i<=n;i++)
    25     {
    26         if(vis[i]) continue;
    27         if(a[i]+len<ans)
    28         {
    29             vis[i]=true;
    30             if (dfs(num+1,i+1,len+a[i])) return true;//如果dfs(used+1,i+1,len+a[i])返回false,则第i根不可用,标记为false  
    31             vis[i]=false;
    32             while((a[i]==a[i+1])&&((i+1)<n))  //长度为a[i]的不能匹配,则和它同样长的不需要再匹配。
    33             {
    34                 i++;
    35             }
    36             if (len==0) return false;//如果当前长度为0,证明没有合适的,return false 
    37         } else
    38         if(a[i]+len==ans)
    39         {
    40             vis[i]=true;
    41             if (dfs(num+1,1,0)) return true;
    42             vis[i]=false;
    43             return false;
    44         }
    45     }
    46     return false;
    47         
    48 }
    49             
    50 void
    51 qsort(int head,int tail)
    52 {
    53     int i,j,x;
    54     i=head;j=tail;
    55     x=a[head];
    56     while(i<j)
    57     {
    58         while((i<j)&&(a[j]<=x)) j--;
    59         a[i]=a[j];
    60         while((i<j)&&(a[i]>=x)) i++;
    61         a[j]=a[i];
    62     }
    63     a[i]=x;
    64     if (head<(i-1)) qsort(head,i-1);
    65     if ((i+1)<tail) qsort(i+1,tail);
    66 }
    67 
    68 int 
    69 main()
    70 {  
    71     
    72  while(~scanf("%d
    ",&n))
    73  {
    74      pre();
    75      if(n==0) break;
    76     for(i=1;i<=n;i++)
    77     {
    78         scanf("%d",&a[i]);
    79         sum+=a[i];
    80     }
    81      
    82    qsort(1,n); //按木棍长度从大到小排序。
    83     maxi=a[n];
    84     for(i=n;i>=1;i--)
    85     {
    86         if ((sum%i==0)&&((sum/i)>=maxi))
    87         {
    88             ans=sum/i;
    89             memset(vis,false,sizeof(vis));
    90             if (dfs(0,1,0)) 
    91             { 
    92                 printf("%d
    ",ans);
    93                 break;
    94             }
    95         }
    96     }
    97  }
    98     return 0;
    99 }
  • 相关阅读:
    201. Bitwise AND of Numbers Range
    200.Number of Islands
    199. Binary Tree Right Side View
    198. House Robber
    191. Number of 1 Bits
    190. Reverse Bits
    odoo pivot filed字段设置
    postgres 实现查找所有的子记录,child_of
    postgres 查询返回记录集的函数
    python GUI编程/窗口编程之easygui
  • 原文地址:https://www.cnblogs.com/sxiszero/p/3632543.html
Copyright © 2011-2022 走看看