Sticks题解

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
本题的两个剪枝:
1、从大到小排序后,从大的为起点开始搜,因为从小的开始搜,组合情况会很多,组合成一个棍子需要搜索很多情况,这个很重要。
2、当前组合好的部分和一根长度为a的棍子组合,若他们的长度大于sLength,那么下次组合不再和长度为a的棍子进行组合。

补充的测试数据:
6
1 2 2 2 2 3
9
15 3 2 11 4 1 8 8 8

#include <iostream>
#include <stdio.h>
#define MAXNumPart 66
#define MAXUnit 53
using namespace std;
int numPart,mNumPart,sLength,alLength,mLength;
int part[MAXNumPart];
int maxPart,minPart;
bool Road=false;

void dfs(int now)
{
    if(mLength<sLength)
    {
        for(int i=now;i>=minPart;i--)
        {
            if(part[i]>0)
            {
                part[i]--;
                mLength+=i;
                mNumPart--;
                dfs(i);
                if(Road)
                    return;
                part[i]++;
                mLength-=i;
                mNumPart++;
//                if(mLength+i>sLength)
//                {
//                    break;
//                }
            }
        }
    }
    else
    {
        if(mLength==sLength)
        {
            if(mNumPart!=0)
            {
                for(int i=maxPart;i>=minPart;i--)
                {
                    if(part[i]>0)
                    {
                        part[i]--;
                        mNumPart--;
                        mLength=i;
                        dfs(i);
                        if(Road)
                            return;
                        part[i]++;
                        mNumPart++;
                        mLength=sLength;
                        break;
                    }
                }
            }
            else
                Road=true;
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    int temp;
    while(~scanf("%d",&numPart)&&numPart!=0)
    {
        mNumPart=numPart;
        alLength=0;
        maxPart=0;
        minPart=MAXUnit-1;
        Road=false;
        for(int i=0;i<MAXNumPart;i++)
            part[i]=0;
        for(int i=0;i<numPart;i++)
        {
            scanf("%d",&temp);
            alLength+=temp;
            part[temp]++;
            if(maxPart<temp)
                maxPart=temp;
            if(temp<minPart)
                minPart=temp;
        }
        for(sLength=maxPart;sLength<=alLength;sLength++)
        {
            if(alLength%sLength==0)
            {
                part[maxPart]--;
                mNumPart--;
                mLength=maxPart;
                dfs(maxPart);
                if(mNumPart==0)
                {
                    printf("%d\n",sLength);
                    break;
                }
                else
                {
                    part[maxPart]++;
                    mNumPart++;
                    mLength=0;
                }
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值