zoukankan      html  css  js  c++  java
  • HDU 1455 Sticks(经典剪枝)

    Sticks

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 14667    Accepted Submission(s): 4517


     

    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
     
     


     

    Source
     


     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1258 1312 1067 1010 1016 

     

    【题意】

    剪成n个短柴柴,让你求这些短柴柴组合成原来长度的木棍的最短长度。

    <==>

    n个数分成m堆,每一堆的和都相等为H,求H最小为多少。

     

     

    【分析】

     

    题中有一些数字:

     

    50:指剪后每一段柴柴长度最大为50

     

    64:指剪后柴柴数量最大为64

     

    还有注意,有可能有柴柴根本就没有减。

     

    比如:

     

    5

     

    1 2 3 4 5

     

    最短的就是  1+4=2+3=5   

     

    初始就是3根长度为5的柴柴,题目没要求求多少根,只求长度最小为多少。

     

     

    做法:按照深搜步骤来就行,就是注意一下一些地方的剪枝

     

    主要是dfs函数中那三个if里的剪枝。

     

    第一条剪枝:

     

    如果你剩余的长度和当前长度相等,就没有必要搜索,也就是说当你剩余长度为3,接着搜索3,发现不符合,就不需要搜索剩下能构成3的(比如2+1,1+1+1等)

     

    第二条剪枝:(重要的)

     

    意思是如果剩余的长度等于柴柴总长度,而当前不符合,就不需要接着搜索。

     

    也就是说,接下来搜索的长度就是柴柴目标长度,而你当前长度根本用不上,那就肯定不符合了。

     

    例子:  假设 要搜索目标长度为 7  :绳长有 7 6 3 2 2.

     

    假设 第一个7 搜索完,接下来搜索6 发现6没有1来配对,程序会接下来搜索 3 2 2,但很明显根本不需要搜索后面的,前面有一个用不上,后面就不需要再搜索了。

     

    第三条剪枝:

     

    如果当前长度的不满足,那么相同长度的就不需要再次去搜索

     

     

     

    【代码】

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=70;
    int n,sum,ans,a[N];bool vis[N];
    inline void Init(){
    	sum=0;
    	for(int i=1;i<=n;i++) scanf("%d",a+i),sum+=a[i];
    	sort(a+1,a+n+1,greater<int>());
    }
    bool dfs(int res,int cou){//(rest of length,counts of what are chosen)
    	if(cou==n) return 1;
    	if(res==0&&cou<n) if(dfs(ans,cou)) return 1;
    	for(int i=1;i<=n;i++){
    		if(!vis[i]&&a[i]<=res){
    			vis[i]=1;
    			if(dfs(res-a[i],cou+1)) return 1;
    			vis[i]=0;
    			if(res==a[i]) return 0;//1
    			if(res==ans) return 0;//2
    			for(;a[i]==a[i+1];i++);//3
    		}
    	}
    	return 0;
    }
    inline void Solve(){
    	for(ans=a[1];1;ans++){
    		for(;sum%ans;ans++);
    		memset(vis,0,sizeof vis);
    		if(dfs(ans,0)){
    			printf("%d
    ",ans);
    			return ;
    		}
    	}	
    }
    int main(){
    	while(~scanf("%d",&n)&&n){
    		Init();
    		Solve();
    	}
    	return 0;
    }

     

     

     

     

  • 相关阅读:
    Core的学习三:容器的使用实例
    Core的学习二:【四步完成】.Net Core中Log4net的使用,配置,【框架.NET5】
    C#7 的一些新语法
    C#6 的一些新语法
    Core的学习一:Core部署在IIS下
    C# 特性【Attribute】【什么是特性?以及特性的一些修饰】
    C#反射
    泛型 -Generic 【why,原理,与普通方法,object的性能对比如何?泛型类、泛型方法、泛型接口、泛型委托,泛型约束,协变 逆变,泛型缓存】
    springboot通过切面编程实现系统请求操作日志记录
    Linux 【安装配置VM虚拟机】
  • 原文地址:https://www.cnblogs.com/shenben/p/10380671.html
Copyright © 2011-2022 走看看