HDU 1455 Sticks

Sticks

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


 

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 1067 1016 1045 1426 
 
 
 
题意:初始时有一些长度相等的木棒,数量未知。随机将其分割,切割后的每份长度都不多于50。现在需要通过切割后的木棒长度序列,求出原来木棒的长度的最小值。
思路:首先,可以想到可以使用dfs进行枚举长度进行搜索判断,但是木棒数量太多,单纯dfs肯定爆时间,于是就需要 剪枝
 
1. 计算所有切割后所有木棒的总长sum,然后将这些木棒sticks 按照长度降序排列,得到单根木棒长度最大值maxlen
2. 确定枚举长度的范围为[maxlen, sum],但是可以进一步缩小为[maxlen, sum/2],因为如果超过sum/2,那么明显只能得到原来木棍的长度为sum,故不需要重复计算。
3. 对于枚举范围内的长度len进行搜索判断,设置参数len ,left, stick,并设置一个visit数组标记木棍是否已被选择使用
// len: 原始stick长度,left: 还需多少长度拼成len长度, stick: 剩余没有使用过的stick

 

    以下是搜索的步骤:
    1)若stick = 0 && left = 0,说明匹配结束,所有木棒均被选择过,并且完整拼接,返回true;
    2)    否则遍历所有木棒,选择未被选择过并且长度小于当前剩余长度left的木棒;
    3)        若当前选择木棒与前一根木棒长度相同并且前面一根木棒未匹配成功,那么这根木棒也不会匹配成功,剪枝跳过;
    4)        否则选择当前木棒进行dfs搜索,并更新参数left为left - sticks[i](当前选中木棒的长度),stick减一,visit[i]=true
    5)            若上面dfs搜索的返回值为true,返回true即可
    6)            否则将visit[i]标记为false
    7)        经过上面的匹配仍未成功,若sticks[i] == left,说明当前木棒刚好与其匹配,但是剩下的木棒仍然无法完成匹配,说明剩下的木棒无法匹配成若干根长度为len的木棒,即剩下的木棒加上当前木棒组成的木棒集合是无法完成匹配的;另外若left = len,可以看成是上面一种情况状态下再次进行了一次递归,也就是从剩余的长度重新为len的情况下仍然无法匹配,那么也就在其他情况下也无法完成匹配了,直接返回false即可。
    8)        否则进入3继续循环
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值