首页 > 其他 > 详细

poj 1011 Sticks

时间:2014-05-30 23:01:15      阅读:525      评论:0      收藏:0      [点我收藏+]

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

确实是一道好题啊,剪枝有几个地方,不过感觉都是很自然的剪枝,没有什么特别牛的地方,但是剪枝与不剪枝差别真的很大
bubuko.com,布布扣
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;

bool cmp(int x,int y)
{
    return x>y;
}

int a[65],n;
bool vis[65];

bool dfs(int nowlen,int Len,int x,int num)
{
    if (num==n) return true;
    int temp=0;
    for (int i=x;i<n;i++)
    {
        if (vis[i] || a[i]==temp) continue;
        vis[i]=true;
        if (nowlen+a[i]<Len)
        {
            if (dfs(nowlen+a[i],Len,i+1,num+1)) return true;
            else temp=a[i];
        }
        if (nowlen+a[i]==Len)
        {
            if (dfs(0,Len,0,num+1)) return true;
            else temp=a[i];
        }
        vis[i]=false;
        if (nowlen==0) break;
    }
    return false;
}

int main()
{
    int sum,maxx;
    bool flag;
    while (scanf("%d",&n)!=EOF && n)
    {
        memset(vis,0,sizeof(vis));
        sum=0;
        flag=false;
        for (int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a,a+n,cmp);
        maxx=a[0];
        for (int i=maxx;i<=sum-i;i++)
        {
            if (sum%i==0 && dfs(0,i,0,0))
            {
                flag=true;
                printf("%d\n",i);
                break;
            }
        }
        if (!flag) printf("%d\n",sum);
    }
    return 0;
}
bubuko.com,布布扣

作者 chensunrise

poj 1011 Sticks,布布扣,bubuko.com

poj 1011 Sticks

原文:http://www.cnblogs.com/chensunrise/p/3760056.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!