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

    Sticks
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 106254   Accepted: 24234

    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 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 
     6 using namespace std;
     7 
     8 int sticks[65],vis[65];
     9 int n,len;
    10 
    11 int DFS(int i,int l,int t){ //i为当前试取的棍子序号,l为要拼成一根完整的棍子还需要的长度,t初值为所有棍子总长度 
    12     if(l==0){
    13         t-=len;
    14         if(t==0)
    15             return 1;
    16         for(i=0;vis[i];i++) ;    //剪枝1:搜索下一根大棍子的时候,找到第一个还没有使用的小棍子开始
    17         vis[i]=1;   //由于排序过,找到的第一根肯定最长,也肯定要使用,所以从下一根开始搜索
    18         if(DFS(i+1,len-sticks[i],t))
    19             return 1;
    20         vis[i]=0;
    21         t+=len;
    22     }else{
    23         for(int j=i;j<n;j++){
    24             if(j>0 && (sticks[j]==sticks[j-1] && !vis[j-1]))     //剪枝2:前后两根长度相等时,如果前面那根没被使用,也就是由前面那根
    25                 continue;   //开始搜索不到正确结果,那么再从这根开始也肯定搜索不出正确结果,此剪枝威力较大
    26             if(!vis[j] && l>=sticks[j]){    //剪枝3:最简单的剪枝,要拼成一根大棍子还需要的长度L>=当前小棍子长度,才能选用                 
    27                 l-=sticks[j];
    28                 vis[j]=1;
    29                 if(DFS(j,l,t))
    30                     return 1;
    31                 l+=sticks[j];
    32                 vis[j]=0;
    33                 if(sticks[j]==1)    //剪枝4:威力巨大的剪枝,程序要运行到此处说明往下的搜索失败,若本次的小棍长度刚好填满剩下长度,但是后面的搜索失败,则应该返回上一层
    34                     break;
    35             }
    36         }
    37     }
    38     return 0;
    39 }
    40 
    41 int cmp(int a,int b){
    42     return a>b;
    43 }
    44 
    45 int main(){
    46 
    47     //freopen("input.txt","r",stdin);
    48 
    49     while(~scanf("%d",&n) && n){
    50         int sum=0;
    51         for(int i=0;i<n;i++){
    52             scanf("%d",&sticks[i]);
    53             sum+=sticks[i];
    54         }
    55         memset(vis,0,sizeof(vis));
    56         sort(sticks,sticks+n,cmp);  //剪枝5:从大到小排序后可大大减少递归次数
    57         int flag=0;
    58         for(len=sticks[0];len<=sum/2;len++){    //剪枝6:大棍长度一定是所有小棍长度之和的因数,且最小因数应该不小于小棍中最长的长度
    59             if(sum%len==0){
    60                 if(DFS(0,len,sum)){
    61                     flag=1;
    62                     printf("%d\n",len);
    63                     break;
    64                 }
    65             }
    66         }
    67         if(!flag)
    68             printf("%d\n",sum);
    69     }
    70     return 0;
    71 }
  • 相关阅读:
    Mac终端编写c语言程序方法
    X-code最常用快捷键
    类方法和实例方法区别
    一、SQL语句中的增、删、查、改
    从零开始,学习web前端之HTML基础
    图片 自适应 容器大小
    Java第二天 数据类型
    Java 第一天
    javascript的基础知识
    JavaScript(一)
  • 原文地址:https://www.cnblogs.com/jackge/p/3023184.html
Copyright © 2011-2022 走看看