zoukankan      html  css  js  c++  java
  • 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

    至少做到我努力了
  • 相关阅读:
    小、快、简、易、强的“银弹”— fastm
    使用Creative suite 3和Flex Builder3实现Flex 3的换肤
    Apache HTTP Server 与 Tomcat 的三种连接方式介绍
    iframe自动适应付窗口的大小变换
    Flash网络游戏开发入门经验共享
    比较详细的 Linux Top 命令解析
    HttpContext是干什么的
    asp.net,cookie,写cookie,取cookie 的方法
    为什么我们不要 .NET 程序员
    在Ubuntu上安装使用深度影音&深度音乐(推荐)
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3760056.html
Copyright © 2011-2022 走看看