Sticks
Description
George took sticks of the same length and cut them randomly until all parts became at most units long. Now he wants to return sticks to the original state, but he forgot what many sticks he had originally and how long they wer E originally. Him and design a program which computes the smallest possible original length of those sticks. All lengths expressed in units is integers greater than zero.
Input
The input contains blocks of 2 lines. The first line contains the number of sticks parts after cutting, there is at most 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 should contains the smallest possible length of original sticks, one per line.
Sample Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
Source
Chinese test Instructions
George had some of the same long little sticks, and he cut the sticks into pieces at random, until the length of each paragraph was no more than 50.
Now, he wants to splice the sticks into the original, but forgets how many sticks and lengths they had at the beginning.
given the length of each small stick, programming helps him figure out the minimum possible length of the original stick.
Outputonly one row, which represents the minimum possible length of the requested original stick.
Ideas
First, the small stick length from the big to the small sort, from the longest stick aim to search, if the total length of these sticks to divide the length of the stick aim, began to recursive search.
The number of roots that can be spelled is Sum/aim, and if successful, the current length is output. If it can match successfully retrun true, otherwise return false.
Mark the searched sticks at the same time and unmark them if they are not available.
If the current stick is not available, then the stick of the same length as the stick can not be used (pruning).
#include <iostream>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;Const intMAXN = -;intN, SUM, aim, NUM, A[MAXN];BOOLUSED[MAXN];BOOLcmpintAintb) {//sort from big to small returnA >b;}BOOLDfsintStick,intLenintPOS) {//Stick Current group good number len Group good length pos search to the first few root BOOLSign = (Len = =0?true:false); if(Stick = = num)return true;//complete the search, exit the function for(inti = pos +1; I < n; i++) { if(Used[i])Continue;//the current stick is no longer available. if(len + a[i] = = aim) {//It 's exactly the same length as the current stick.Used[i] =true; if(Dfs (stick+1,0, -1))return true;//go to the next level searchUsed[i] =false;//Backtracking return false; } Else if(len + a[i] < aim) {//Not enough lengthUsed[i] =true; if(Dfs (Stick, Len + a[i], i))return true;//go to the next layer of search, the stick length becomes Len + a[i]Used[i] =false; //key step, no stick selected, jump out of the loop if(sign)return false; while(A[i] = = a[i+1]) i++;//Pruning } } return false;}intMain () {scanf ("%d", &N); Sum=0; for(inti =0; I < n; i++) {scanf ("%d", &A[i]); Sum+ = A[i];//Find out the total length of all sticks} sort (A, a+n, CMP);//Sort for(AIM = a[0]; Aim <= sum; aim++) { if(sum% aim = =0) {num= sum/aim; memset (Used,false,sizeof(used));//Initialize if(Dfs (1,0, -1) {printf ("%d\n", aim);//Direct Output Answer Break; } } } return 0;}
It's better.
Poj1011 Sticks Small Stick Dfs Search