Sticks hdu 1455

Sticks

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4854    Accepted Submission(s): 1362


Problem 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 file 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
 
 
 
 

   这个题 我感觉是十分经典的 ,因为其中用到了很多剪枝的手法,今天我意识到剪枝并不是一个固定的运算,而是

 一种思维上的技巧,是通过严密的思考来省略很多繁多的运算。

 

  在这个题目中,用到了三种剪枝,第一是所得的筷子长度必须是其总长度约数,好理解,第二个重复的不用在算了,好理解,第三个是你选中一个筷子为组成木块长度时,如果它不行,就不用再进行下一个了,直接返回0就足够了

 

 剪枝:如果当前搜索时,前边的长度为0,而第一根没有成功的使用,
 说明第一根始终要被废弃,所以这种组合必定不会成功

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int total,l;
int n,sum;
struct info
{
    int length;
    int maze;
} unit[100];
int cmd(info x,info y)
{
    return x.length>y.length;
}
int dfs(int s,int len,int pos)
{
    if(s==total)
        return 1;
    int i;
    for(i=pos+1; i<n; i++)
    {
        if(unit[i].maze)
            continue;
        if(len+unit[i].length==l)
        {
            unit[i].maze=1;
            if(dfs(s+1,0,-1))
                return 1;
            unit[i].maze=0;
            return 0;


        }
        else if(len+unit[i].length<l)
        {
            unit[i].maze=1;
            if(dfs(s,len+unit[i].length,i))
                return 1;
            unit[i].maze=0;
            if(len==0)
                return 0;
//这个不行,再往下算就没意义了,直接返回为0
            while(unit[i].length==unit[i+1].length)
//重复的不要算
                i++;


        }

    }



    return 0;
}



int main()
{
    int i,j,k;
    while(scanf("%d\n",&n)!=EOF)
    {
        if(n==0)
            break;
        sum=0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&unit[i].length);
            unit[i].maze=0;
            sum=sum+unit[i].length;
        }
        sort(unit,unit+n,cmd);
        for(l=unit[0].length; l<=sum; l++)
        {
            if(sum%l!=0)
                continue;
            total=sum/l;
            if(dfs(1,0,-1))
            {
                printf("%d\n",l);
                break;
            }
        }

    }
    return 0;
}


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值