zoukankan      html  css  js  c++  java
  • hdu 1455 Sticks

    Sticks
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    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 file 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<iostream>
    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int st[65];
    bool vis[65];
    int cnt;
    int n;
    int l;
    bool cmp(int a,int b)
    {
        return a>=b;
    }
    bool dfs(int num,int len)
    {
        if(num==cnt) return true;
        for(int i=0;i<n;i++)
        {
            if(!vis[i])
            {
                if(len+st[i]==l)
                {
                    vis[i]=true;
                    if(dfs(num+1,0)) return true;
                    vis[i]=false;
    
                }
                if(len+st[i]<l)
                {
                    vis[i]=true;
                    if(dfs(num,len+st[i])) return true;
                    vis[i]=false;
                    if(len==0) return 0;//这里剪枝很重要,而且很经典。如果当前为0,即本次搜索失败,则一定会废弃一个static,则当前长度的分法肯定不成立。
                    int last=st[i];
                    while(last==st[i]) i++;
                }
            }
        }
        return false;
    }
    int main()
    {
    
        while((scanf("%d",&n))&&(n!=0))
        {
            for(int i=0; i<=n; i++) vis[i]=false;
            int sum=0;
            cnt=0;
            for(int i=0; i<n; i++)
            {
                scanf("%d",&st[i]);
                sum+=st[i];
            }
            sort(st,st+n,cmp);
            for(int i=st[0];i<=sum;i++)
            {
    
                if(sum%i==0)
                {
                    //cout<<"fuck"<<endl;
                    cnt=sum/i;
                    l=i;
                    if(dfs(0,0))
                    {
                        printf("%d
    ",l);
                        break;
                    }
                }
            }
        }
    }
    View Code
  • 相关阅读:
    2015 年最受 Linux 爱好者欢迎的软硬件大盘点
    Java 9终于要包含Jigsaw项目了
    Linux 容器技术史话:从 chroot 到未来
    开发者最常用的 8 款 Sublime Text 3 插件
    60,000毫秒内对Linux的性能诊断效的方法
    bzoj 2595 [Wc2008]游览计划(斯坦纳树)
    bzoj 3997 [TJOI2015]组合数学(DP)
    bzoj 1014 [JSOI2008]火星人prefix(splay+hash)
    bzoj 1090 [SCOI2003]字符串折叠(区间DP)
    bzoj 1537 [POI2005]Aut- The Bus(DP+BIT)
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5720425.html
Copyright © 2011-2022 走看看