Sticks题解

来源:互联网 发布:领啦试用网站源码 编辑:程序博客网 时间:2024/05/29 14:58

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 53using 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;}
原创粉丝点击