Sticks
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 126238 |
|
Accepted: 29477 |
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
UVA above the commit timeout, POJ can be over;
AC Code:
1#include <algorithm>2#include <cstring>3#include <cstdio>4#include <iostream>5 using namespacestd;6 Const intN = 1e4+Ten;7 intlen[n],sum,l,t;8 intUsed[n];9 //BOOL cmp (int x,int y)Ten //{ One //if (x>y) return true; A //} - intcmpConst void*a,Const void*b) - { the return*(int*) b-* (int*) A; - } - BOOLDFS (intMintLeft//m is the remaining number of sticks, left for the currently stitching stick and the assumed length of the stick length L also missing - { + if(M = =0&& left = =0) - return true; + if(left = =0)//I just finished the fight . Aleft =L; at for(intI=0; i<t; i++) - { - if(!used[i] && len[i]<=Left ) - { - if(i>0)//If the former is not available, then the current is not available . - { in if(!used[i-1] && len[i] = = len[i-1]) - Continue; to } +Used[i] =1; - if(DFS (M-1, left-Len[i])) the return true; * Else $ {Panax NotoginsengUsed[i] =0; - if(Len[i] = = Left | |L) the return false; + } A } the } + return false; - } $ intMain () $ { - while(SCANF ("%d", &t) &&T) -{sum =0 ; the for(intI=0; i<t;i++) - {Wuyiscanf"%d",&len[i]); thesum = sum +Len[i]; - } Wu //sort (len,len+t,cmp); Sort Timeout -Qsort (Len,t,sizeof(int), CMP);//sort from big to small About for(L = len[0]; l<=sum/2; l++) $ { - if(sum%l)Continue; -memset (Used,0,sizeof(used)); - if(DFS (t,l)) A { +printf"%d\n", L); the Break; - } $ } the if(l>sum/2) printf ("%d\n", sum); the } the return 0; the}
POJ 1011 Sticks