• 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

    确实是一道好题啊,剪枝有几个地方,不过感觉都是很自然的剪枝,没有什么特别牛的地方,但是剪枝与不剪枝差别真的很大
    #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
    ",i);
                    break;
                }
            }
            if (!flag) printf("%d
    ",sum);
        }
        return 0;
    }

    作者 chensunrise

    至少做到我努力了
  • 相关阅读:
    Spark GraphX 属性图操作
    Scala隐式转换
    Spark GraphX
    交叉验证_自动获取模型最优超参数
    协同过滤算法
    聚类-----高斯混合模型
    IDEA kafka producer数据输出缓慢 和 kafka consumer 报错的处理
    Opencv读取视频一闪而过情况分析
    影像叠加+滑动条
    opencv中的图像矩(空间矩,中心矩,归一化中心矩,Hu矩)
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3760056.html
Copyright © 2020-2023  润新知