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;
    }
    
  • 相关阅读:
    Struts2学习笔记《三》
    《Shiro框架》shiro学习中报错解决方法
    android
    MAC 设置环境变量path的几种方法
    利用ant脚本 自动构建svn增量/全量 系统程序升级包
    Jenkins2 插件 Pipeline+BlueOcean 实现持续交付的初次演练
    Jenkins2 实现持续交付初次演练(MultiJob,Pipeline,Blue Ocean)
    jenkins2 -pipeline 常用groovy脚本
    jenkins2 pipeline介绍
    scala学习(1)----map和flatMap的区别
  • 原文地址:https://www.cnblogs.com/cssystem/p/2842002.html
Copyright © 2011-2022 走看看