zoukankan      html  css  js  c++  java
  • 4.3.5 Sticks (POJ1011)

    Problem 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

    思路:dfs+强力剪枝,具体怎么做的我都记不得了,参考了别人的代码,才发现自己写的有多臭

    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    
    int sum=0,maxx,n,step,he,total;
    bool succ,f[66],flag,rec[66];
    int a[66];
    
    void close()
    {
    exit(0);
    }
    
    bool check()
    {
    for (int i=0;i<n;i++)
    	if (not f[i]) return false;
    return true;
    }
    
    
    bool dfs(int tot,int k,int cnt)
    {
    if (step==200 || cnt==total)
    {
    	flag=true;
    	return true;
    }
    	for (int j=k;j>=0;j--)
    	{
    		if (not f[j])
    		{
    				if (a[j]+tot==he)
    				{
    			   	f[j]=true;
    					return true;
    					f[j]=false;
    					return false;
    				}
    				else if (a[j]+tot<he)
    				{
    			   	f[j]=true;
    					if (dfs(tot+a[j],j,cnt))
    						return true;
    					f[j]=false;
    					if (tot==0) return false;
    					while (a[j-1]==a[j]) j--;
    				}
    		}
    	}
    return false;
    }	
    void work()
    {
      for (int i=maxx;i<=sum;i++)
      {
    	  succ=false;
    	  if (sum % i==0)
    	  {
    		  total=sum/i;
    		  memset(f,false,sizeof(f));
    		  flag=false;
    		  he=i;
    		  step=0;
    		  if (dfs(0,n-1,1))
    		  {
    			  printf("%d\n",i);
    			  return ;
    			}
    		  if (check())
    		  {
    			  printf("%d\n",i);
    			  return ;
    		  }
    	  }
      }
    }
    
    
    void init()
    {
      while (scanf("%d",&n)!=EOF)
      {
    	  memset(f,false,sizeof(f));
    	  sum=0;maxx=0;
    	  if (n==0) break;
    	  for (int i=0;i<n;i++)
    	  {
    		  scanf("%d",&a[i]);
    		  sum+=a[i];
    		  maxx=max(a[i],maxx);
    	  }
    	  sort(a,a+n);
    	  work();
      }
    }
    
    
    int main ()
    {
    init();
    close();
    return 0;
    }
    
  • 相关阅读:
    Hadoop2.x集群动态添加删除数据节点
    HDFS中块状态分析
    procedure of object(一个特殊的指针类型)
    设计模式导语一
    Delphi中的容器类(二)
    让AlphaControls改变DevExpress皮肤
    Delphi中的容器类(一)
    Delphi 的RTTI机制浅探
    重写AuthorizeAttribute实现自己的权限验证
    含有HttpContext元素的单元测试
  • 原文地址:https://www.cnblogs.com/cssystem/p/2842002.html
Copyright © 2011-2022 走看看