Poj (1011)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

问题分析:

如果是简单地输入,即使做了一些无谓的搜索,也能够求出结果。但是,由于切出来的段数最多有64根,单纯计算所有组合就需要很长时间。这次用一个稍微复杂一点的例子,首先手动求组合看看。

首先,准备下面这样的测试数据。

8 3 8 15 2 11 4 8 1

这9个数值的合计(sum)是60,最大值(max)是15,60的约数中比15大的有15、20、 30、60(=length)这4个,检查切出来的全部片段是不是能够拼出这些长度的棒子。

首先考虑length=15的情况。

将这9个数字降序排列:

15 11 8 8 8 4 3 2 1

从这个清单的开头开始,分割成合计为15的部分。

15

11,4

8,…

15保持原状,下一个11+4=15,再下一个8加上8就是16 了,超出了 15。即使依次加上3、 2、1还是等于14,凑不上15。所以知道原本棒子的长度不是15。那么下面这组又怎么样呢?

15

11,3, 1

8,…

同前面那组一样无法凑成15,似乎原本棒子的长度不是15。 20能行吗?

15, 4, 1

11,8,…

最初选择{15, 4, 1}就不能拼出第2根棒子了,但是这就说20不行有些太早了。

15,3,2

11,8, 1

8, 8, 4

如果最开始的一组用{15, 3, 2},就能顺利凑齐长度是20的棒子。接下来把这样的搜索方法用程序代码来实现就可以了。


#include<iostream>
#include<algorithm>
using namespace std;
bool visit[100];
int goal,sum,n;
int sticks[100];
bool cmp(int a,int b)
{
    return a>b;
}
bool dfs(int now,int index,int cnt)//now当前已组装木棍的长度,index已经用到第几根木棍,cnt已经组装好的木棍
{
    if(goal*cnt == sum)//所有木棍都已组装好
    {
        return true;
    }
    for(int i=index;i<n;i++)
    {
        if(visit[i]||(i&&!visit[i-1]&&sticks[i]==sticks[i-1]))//木棍已经使用或者与之前不可行木棍长度相同直接跳过
        {
            continue;
        }
        if(now+sticks[i]==goal)//当前木棍组装成功
        {
            visit[i] = true;//木棍已使用
            if(dfs(0,0,cnt+1))//得到可行解
            {
                return true;
            }
            visit[i]=false;//回溯
            return false;//未得到可行解
        }
        else if(now+sticks[i]<goal)//加上此木棍长度
        {
            visit[i] = true;
            if(dfs(now+sticks[i], i+1, cnt))//得到可行解
            {
                return true;
            }
                visit[i] = false;
                if(now==0)//正在组合的木棍的第一根木棍已经无法得到可行解
                    return false;
        }
    }
    return false;
}
int solve()
{
    sort(sticks,sticks+n,cmp);
    for(goal=sticks[0];goal<=sum;goal++)
    {
        if(sum%goal!=0)
        {
            continue;
        }
        memset(visit, false, sizeof(visit));
        if(dfs(0,0,0))
        {
            return goal;
        }
    }
    return 0;
}
int main()
{
    while(cin>>n)
    {
        sum=0;
        if(n==0)
        {
            break;
        }
        for(int i=0;i<n;i++)
        {
            cin>>sticks[i];
            sum+=sticks[i];
        }
        cout<<solve()<<endl;
    }
    return 0;
}

测试数据:

最后再给出一组 Test Case,也是北京大学ACM论坛上的,请大家参考:

测试用例:

9
15 3 2 11 4 1 8 8 8

结果:20

6
6 2 2 4 8 8
结果:10
5
1 1 1 1 1 
结果:1
2
1 1
结果:1
4
2 2 9 9
结果:11
3
1 2 3
结果:3
64
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40 
40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
40 
结果:1251
7
49 48 47 46 45 44 43
结果:322
7
3 4 5 5 5 5 13
结果:20
7
2 7 7 7 7 10 20
结果:30
6
1 2 3 11 11 20
结果:24
7
63 2 44 12 60 35 60 
结果:276
9
5 2 1 5 2 1 5 2 1
结果:6
4
1 2 3 4
结果:5
64
32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32
32 32 32 32 32 32 32 32 32 32
33 33 31 31
结果:64
64
40 40 30 35 35 26 15 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
40 40 43 42 42 41 10 4 40 40 40 40 40 40 40 40 40 40 40 40 40 
40 25 39 46 40 10 4 40 40 37 18 17 16 15 40 40 40 40 40 40 40 
40
结果:454

45
15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8 15 3 2 11 4 1 8 8 8
15 3 2 11 4 1 8 8 8
0
结果:20

猜你喜欢

转载自blog.csdn.net/weixin_42103959/article/details/80790710