zoukankan      html  css  js  c++  java
  • UVA307Sticksdfs+剪枝

    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. 如果拼起来的长度大于你所要的木棍长度的话,需要继续判断,因为可能和之后的可以拼在一起,所以这里需要分情况考虑。

     

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

  • 相关阅读:
    Java基础——Instanceof 运算符
    算法——八皇后问题(eight queen puzzle)之回溯法求解
    浅析数据结构
    react給變量賦值并列元素
    如何使用npm构建一个react demo项目
    Java面试题 静态代码块 构造代码块 构造方法 的执行顺序
    Mysql 反向解析 导致远程访问慢
    Django admin管理工具
    Django-Ajax(85)
    jQuery快速入门
  • 原文地址:https://www.cnblogs.com/OFSHK/p/11222065.html
Copyright © 2011-2022 走看看