UVA-307-Sticks-dfs + pruning

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

 

The meaning of problems: a given length n n represents the root of a small wooden stick, n number of these freely combined as small sticks and a minimum length of the stick pieced together.

Ideas: the need for multiple pruning

  1. The length of the stick will be the sum of all of these factors the block, but the length must be greater than all the divided wooden sticks good longest stick.
  2. The stick descending order, because it faster search whether the length of the stick out can be spelled out.
  3. If it is found that the current wooden block length and the latter is no way to spell out the stick, then if i-1 and the current length of the stick may not be equal, then it is determined that the stick.
  4. If you put together a length greater than the length of the stick you want it, we need to continue to judgment, because after possible and can stand together, so there need to divide the case into account.

 

 

But I did not write the code. . .

Guess you like

Origin www.cnblogs.com/OFSHK/p/11222065.html