木棍

George took sticks of the same length and cut them randomly until all parts became at most 50 unitslong. Now he wants to return sticks to the original state, but he forgot how many sticks he had originallyand how long they were originally. Please help him and design a program which computes the smallestpossible original length of those sticks. All lengths expressed in units are integers greater than zero.InputThe 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 filecontains ‘0’.OutputThe output file contains the smallest possible len
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k,len;
int num[100010],book[100010];
int cmp(int x,int y)
{
    return x>y;
}
int dfs(int cnt,int p,int j)
{
    if(cnt==len)
    {
        cnt=0;
        p++;
        j=0;
    }
    if(p==k)
    {
        return 1;
    }
    for(int i=j+1;i<n;i++)
    {
        if(!book[i]&&cnt+num[i]<=len)
        {
            if(!book[i-1]&&num[i]==num[i-1])continue;
            book[i]=1;
            if(dfs(num[i]+cnt,p,i))return 1;
            book[i]=0;
            if(cnt==0)return 0;
        }
    }
    return 0;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        memset(book,0,sizeof(book));
        int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
            sum+=num[i];
        }
        sort(num,num+n,cmp);
        for(len=num[0];len<=sum/2;len++)
        {
            if(sum%len==0)
            {
                book[0]=1;
                k=sum/len;
                if(dfs(num[0],0,0))break;
            }
        }
        if(len<=sum/2)printf("%d\n",len);
        else printf("%d\n",sum);
    }

}

gth of original sticks, one per line.Sample
 Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output 
65
//有n个木棍,段成许多部分,每根木棍的长度是一样的,要找出原来最短是多少根木棍,要利用深搜的减值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值