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;
    }
    
  • 相关阅读:
    SICP学习笔记 第二章 (2.3)
    SICP学习笔记 第二章 (2.4)
    SICP学习笔记 第二章 (2.2)(上)
    SICP学习笔记 第一章 (1.3)
    SICP学习笔记 第二章 (2.1)
    sql server 获取服务器中数据库的大小
    vss File for <file> (<physical file>) Was Not Found
    Linq 中的获取最大值得记录
    silverlight 报错超时
    asp 中的getChunk(img_size)
  • 原文地址:https://www.cnblogs.com/cssystem/p/2842002.html
Copyright © 2011-2022 走看看