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
  1.  
9 5 2 1 5 2 1 5 2 1 4 1 2 3 4 0
  1.  
 

 

Sample Output
  1.  
6 5
  1.  
 

 

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,但很明显根本不需要搜索后面的,前面有一个用不上,后面就不需要再搜索了。

第三条剪枝:

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


【代码】

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N=70;
  7. int n,sum,ans,a[N];bool vis[N];
  8. inline void Init(){
  9. sum=0;
  10. for(int i=1;i<=n;i++) scanf("%d",a+i),sum+=a[i];
  11. sort(a+1,a+n+1,greater<int>());
  12. }
  13. bool dfs(int res,int cou){//(rest of length,counts of what are chosen)
  14. if(cou==n) return 1;
  15. if(res==0&&cou<n) if(dfs(ans,cou)) return 1;
  16. for(int i=1;i<=n;i++){
  17. if(!vis[i]&&a[i]<=res){
  18. vis[i]=1;
  19. if(dfs(res-a[i],cou+1)) return 1;
  20. vis[i]=0;
  21. if(res==a[i]) return 0;//1
  22. if(res==ans) return 0;//2
  23. for(;a[i]==a[i+1];i++);//3
  24. }
  25. }
  26. return 0;
  27. }
  28. inline void Solve(){
  29. for(ans=a[1];1;ans++){
  30. for(;sum%ans;ans++);
  31. memset(vis,0,sizeof vis);
  32. if(dfs(ans,0)){
  33. printf("%d\n",ans);
  34. return ;
  35. }
  36. }
  37. }
  38. int main(){
  39. while(~scanf("%d",&n)&&n){
  40. Init();
  41. Solve();
  42. }
  43. return 0;
  44. }

 

HDU 1455 Sticks(经典剪枝)的更多相关文章

  1. hdu 1455 Sticks

    Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  2. hdu 1455 Sticks(dfs+剪枝)

    题目大意: George有许多长度相同的木棍,随机的将这些木棍砍成小木条,每个小木条的长度都是整数单位(长度区间[1, 50]).现在George又想把这些小木棒拼接成原始的状态,但是他忘记了原来他有 ...

  3. hdu 1145(Sticks) DFS剪枝

    Sticks Problem Description George took sticks of the same length and cut them randomly until all par ...

  4. poj 1011 Sticks ,剪枝神题

    木棒 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 118943 Accepted: 27429 Description 乔治拿 ...

  5. 经典剪枝算法的例题——Sticks详细注释版

    这题听说是道十分经典的剪枝算的题目,不要问我剪枝是什么,我也不知道,反正我只知道用到了深度搜索 我参考了好多资料才悟懂,然后我发现网上的那些大神原理讲的很明白,但代码没多少注释,看的很懵X,于是我抄起 ...

  6. uva 215 hdu 1455 uvalive5522 poj 1011 sticks

    //这题又折腾了两天 心好累 //poj.hdu数据极弱,找虐请上uvalive 题意:给出n个数,将其分为任意份,每份里的数字和为同一个值.求每份里数字和可能的最小值. 解法:dfs+剪枝 1.按降 ...

  7. hdu 1455(DFS+好题+经典)

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. Sticks HDU - 1455 (未完成)

    George took sticks of the same length and cut them randomly until all parts became at most 50 units ...

  9. poj1011 Sticks(dfs+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110416   Accepted: 25331 Descrip ...

随机推荐

  1. MATLAB错误:下标索引必须是正整数类型或者逻辑类型

    背景: Matlab R2015b 问题: 在运行BP算法时出现错误: 下标索引必须是正整数类型或者逻辑类型 output( i , class( i )  ) = 1 ; 解决办法: 根目录下运行, ...

  2. datepicker clone 控件错误

    删除id,并删除hasDatepicker //+ -  function changeRows(sender,desc){ var tr = $(sender).closest("tr&q ...

  3. 一键切换hosts文件

    1.新建文件host.bat 2.代码 @echo off cd.>C:\Windows\System32\drivers\etc\hosts echo .本地环境 .线上测试环境 ,切换Hos ...

  4. UNIX环境编程学习笔记(10)——文件I/O之硬链接和符号链接

    lienhua342014-09-15 1 文件系统数据结构 UNIX 文件系统通过 i 节点来存储文件的信息.如图 1 所示为一个磁盘柱面上的 i 节点和数据块示意图.其中 i 节点是一个固定长度的 ...

  5. 用Fiddler查看 Android/iOS 网络请求

    1.下载fiddler,尽量到官方网站找最新的版本 我这里也放了一个:http://files.cnblogs.com/xiaokang088/fiddler4setup.zip 2. 打开Fiddl ...

  6. ABBYY FineReader操作技巧

    使用ABBYY FineReader OCR文字识别软件工作即快速又简单,软件自身常常可以自行处理一切工作,用户只需点击几下软件中的‘主要’按钮.不过,有时要想获得更好的质量结果,或者解决某个不寻常的 ...

  7. QT QTransform与QMatrix 有啥区别?

    刚开始学习QT,我使用的是QT5.12进行开发,要不时地查阅QT的官方帮助文档~ 仔细阅读QT官方帮助QTransform类以及QMatrix类,发现两个类的作用描述一模一样(“The QTransf ...

  8. spring 定时任务corn表达式

    * * * * * * *  秒 分 时 日 月 周 年 秒 * / - 0-59 分 * / - 0-59 时 * / - 0-23 * 匹配任意数据 / 每隔多少分钟执行一次 - 区间 案例 0 ...

  9. 禁用滚动视图ListView、ViewPager、ScrollView、HorizontalScrollView、WebView边界颜色渐变

    禁用滚动视图ListView.ViewPager.ScrollView.HorizontalScrollView.WebView边界颜色渐变 ListView.ViewPager.ScrollView ...

  10. MySQL Study之--MySQL普通用户无法本地登陆

    MySQL Study之--MySQL普通用户无法本地登陆       在安装完毕MySQL后,我们通常加入拥有对应权限的普通用户用来訪问数据库.在使用用户本地登录数据库的时候,常常会出现怎么登录也无 ...