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;
    }
    
  • 相关阅读:
    翻译:让网络更快一些——最小化浏览器中的回流(reflow)
    ArcGIS API for flex 3.1离线文档
    innerHeight与clientHeight、innerWidth与clientWidth
    在Excel2010中输入身份证号
    JavaScript window.location对象
    Apache2.2+php5.2+the requested operation has failed
    Js中的window.parent ,window.top,window.self
    Mongoose 3.0 executable does not start
    Download ActionScript 3 reference files as a single zipActionScript 3 下载
    Flex 4.6 API 离线文档
  • 原文地址:https://www.cnblogs.com/cssystem/p/2842002.html
Copyright © 2011-2022 走看看