Stick
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 136132 |
|
Accepted: 32036 |
Description
George brought a set of equal-length sticks and cut them randomly, making each stick less than 50 units in length. He then tried to restore the sticks to a pre-cut state, but forgot how many sticks and the initial lengths of the sticks were initially. Please design a program to help George calculate the possible minimum length of the stick. The length of each stick is represented by an integer greater than 0.
Input
The input contains multiple sets of data, each of which consists of two rows. The first line is an integer that does not exceed 64, indicating how many sticks are in total after the chop. The second line is truncated after the length of each section of the stick. After the last set of data, it is a zero.
Output
For each set of data, output the smallest possible length of the original stick, one row for each group of data.
Sample Input
95 2 1 5 2 1 5 2 141 2 3 40
Sample Output
65
Source
Central Europe 1995 Method: dfs+ Strong pruning Analysis: Classic search problems, need to carefully consider the pruning conditions, each time the pruning can greatly improve the speed. Pruning: 1. First, the data from the big to the small sort, the search time from large to small search, so as to ensure the full use of data, and pruning is also very helpful 2. The length of the original bar is approximate to the total length, so the search space is approximate 3 of the total length. When retrieving, use the length of the longest bar as the starting length of the search 4. If stick[i] = = Stick[i+1], and Stick[i] match fails, then stick[i+1] must also be a failure of 5. If the first bar of group I (also the longest) fails during the matching process, then the I-1 group is returned directly to the re-match. Because the combination of group I must be a subset of the longest wood bars.
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 #defineMax_num 646 #defineFAILURE 17 #defineSUCCESS 08 9 intGwsticknum =1;Ten intGwtotallen; One intGwdata[max_num]; A intGwselect[max_num]; - intGwgroup; - the voidSortint*a,intLeftintRight ) - { - if(Left >=Right ) - { + return; - } + inti =Left ; A intj =Right ; at intKey =A[left]; - while(I <j) - { - while(I<j && key>=A[j]) - { -j--; in } -A[i] =A[j]; to while(I<j && key<=A[i]) + { -i++; the } *A[J] =A[i]; $ }Panax NotoginsengA[i] =key; -Sort (A, left, I-1); theSort (A, i+1, right); + } A the intMatchintGoalintSumintNowgroup) + { - inti =0; $ if(Sum > Goal)returnFAILURE; $ if(Sum = =goal) - { -nowgroup++; the if(Nowgroup = = Gwgroup)returnSUCCESS; -sum =0;Wuyi returnmatch (goal, sum, nowgroup); the } - for(i=1; i<gwsticknum; i++) Wu { - if(Gwselect[i] = =1)Continue; AboutSum + =Gwdata[i]; $Gwselect[i] =1; - if(SUCCESS = = Match (goal, SUM, Nowgroup))returnSUCCESS; -Gwselect[i] =0; -Sum-=Gwdata[i]; A if(Sum = =0)returnFAILURE; + while(Gwdata[i] = = gwdata[i+1] && I <gwsticknum) the { -i++; $ } the } the returnFAILURE; the } the - intSearchfactors (intstart) in { the inti =0; the for(i=start+1; i<=gwtotallen; i++) About { the if(gwtotallen% i = =0)returni; the } the return 0; + } - the intCalc ()Bayi { the intsum = gwdata[0]; the intLen =0; -gwselect[0] =1; - intStart = gwdata[0]-1; the while(Start <Gwtotallen) the { theLen =searchfactors (start); theGwgroup = Gwtotallen/Len; - if(SUCCESS = = Match (Len, Sum,0))returnLen; theStart =Len; thememset (Gwselect,0,sizeof(Gwselect)); thegwselect[0] =1;94sum = gwdata[0]; the } the return 0; the }98 About intGettatollen (int*a,intArrylen) - {101 inti =0;102 intsum =0;103 for(i=0; i<arrylen; i++)104 { theSum + =A[i];106 }107 returnsum;108 }109 the intMainvoid)111 { the inti =0;113 intresult =0; the while(SCANF ("%d", &gwsticknum), gwsticknum! =0) the { the for(i=0; i<gwsticknum; i++)117 {118scanf"%d", &gwdata[i]);119 } - 121Gwtotallen =Gettatollen (Gwdata, gwsticknum);122Sort (Gwdata,0, gwsticknum-1);123result =Calc ();124 theprintf"%d\n", result);126memset (Gwselect,0,sizeof(Gwselect));127 } - 129 return 0; the}
Test data: 915 3 2 8 8 4 each 8 1020------------------------------------------------------2715 3 2 4 11 1 8 8 8 15 3 2 4 11 1 8 8 8 3 2 4 1 8 8 8020------------------------------------------------------2 1 5 2 1 5 2 141 2 3 4065--------------- ---------------------------------------4640 37 32 10 47 4 42 56 61 23 59 36 27 16 16 37 26 19 14 29 31 58 51 32 63 28 11 2 5 12 15 39 42 46 43 11 19 53 17 39 21 45 44 8 23 51 55 5857 6 44 4 16 35 54 9 32 23 43 55 46 41 8 41 55 44 31 59 57 58 59 29 53 30 3 39 52 17 32 45 8 40 34 18 20 11 32 33 14 41 31 25 4 42 54 9 29 37 47 29 34 20 47 56 61 5 263 64 18 49 4 40 18 6 1 50 36 17 49 8 17 62 11 24 8 36 59 34 26 28 7 37 26 0898999
PKU poj-1011