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;
    }
    
  • 相关阅读:
    【后缀数组】
    【后缀数组之height数组】
    【后缀数组之SA数组】【真难懂啊】
    【转】为何浮点数可能丢失精度
    UVa_Live 3664(精度坑)
    【火车出栈】ZOJ
    并发查询
    java基础知识1
    感悟__你总是要建立自己的价值观,世界观,人生观,信念的,总不能一直靠鸡汤,被外界,环境,他人,见闻等所掌控你的情绪,积极或者消极.
    batch、随机、Mini-batch梯度下降
  • 原文地址:https://www.cnblogs.com/cssystem/p/2842002.html
Copyright © 2011-2022 走看看