浏览器标题切换
浏览器标题切换end

UVA-307-Sticks-dfs+剪枝

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 file contains blocks of 2 lines. The first line contains the number of sticks parts after cutting. The second line contains the lengths of those parts separated by the space. The last line of the file contains ‘0’. 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

 

题意:给出一个n代表n根小木棍的长度,将这n根小木棒随意组合拼凑起来的木棍的长度一样且最小。

思路:需要多处剪枝

  1. 木棍的长度一定会是这些所有木块总和的因数,而且木棍长度一定大于所有分割好的木棍中最长的木棍。
  2. 将木棍从大到小排序,因为这样可以快一点搜索出这个木棍的长度是否能够被拼出。
  3. 如果发现当前这个长度的木棍块没有办法和后面的木棍拼出来的话,那么如果i-1和当前的木棍长度相等的话就可以不判断这个木棍了。
  4. 如果拼起来的长度大于你所要的木棍长度的话,需要继续判断,因为可能和之后的可以拼在一起,所以这里需要分情况考虑。

 

 

但是我代码还没写出来。。。

posted @   抓水母的派大星  阅读(194)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示