poj 10010 Sticks bfs+减枝

题目:

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 file contains blocks of 2 lines. The first line contains the number of sticks parts after cutting. 

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

 

题目大意:

n个棍子,每个棍子长ai,用所有的棍子组合成一样长的棒子,求这些棒子最短能多短

题目思路:

因为求最短的,可以从最短的开始测试,但数据很大,所以要减枝。

剪枝:

1  由于所有原始棒子等长,那么必有sumlen%Initlen==0

2  若能在[maxlen,sumlen-InitLen]找到最短的InitLen,该InitLen必也是[maxlen,sumlen]的最短;若不能在[maxlen,sumlen-InitLen]找到最短的InitLen,则必有InitLen=sumlen

3  由于所有棒子已降序排序,在DFS时,若某根棒子不合适,则跳过其后面所有与它等长的棒子;

4  最重要的剪枝:对于某个目标InitLen,在每次构建新的长度为InitLen的原始棒时,检查新棒的第一根stick[i],若在搜索完所有stick[]后都无法组合,则说明stick[i]无法在当前组合方式下组合,不用往下搜索(往下搜索会令stick[i]被舍弃),直接返回上一层

程序:

 

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=10100;
int n;
bool v[maxn];
int a[maxn];
bool cmp(int x,int y)
{
    return x>y;
}
int sou(int sum,int m,int rong,int bao,int w)
{
    // if(m==4)cout<<'!'<<sum<<'\t'<<rong<<bao<<endl;
    if(rong<=0)
    {
        if(bao<=1)
            return 1;
        for(int i=1; i<n; i++)
            if(!v[i])
            {
                v[i]=1;
                int  t=sou(sum,m,sum-a[i],bao-1,i+1);
                if(!t)
                    v[i]=0;
                return t;
            }
    }
    for(int i=w; i<n; i++)//jian 2
    {
        if(!v[i])
            if(a[i]<=rong)
                if(!(i>0&&!v[i-1]&&a[i]==a[i-1]))//jian1
                {
                    v[i]=1;
                    if(sou(sum,m,rong-a[i],bao,i+1))
                        return 1;
                    v[i]=0;
                   // if(rong-a[i]==0)
                    //    return 0; //jian zhi 4
                }
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        int sum=0;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        int da=sum;
        sort(a,a+n,cmp);
        memset(v,0,sizeof(v));
        for(int i=n; i>=2; i--)
            if(sum%i==0)
                if(sum/i>=a[0])
                    if(sou(sum/i,i,sum/i-a[0],i,1))
                    {
                        da=sum/i;
                        break;
                    }
        printf("%d\n",da);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值