This makes me feel weak when I look at the problem-solving report.
Question:
Several wooden sticks of the same length were cut and cut into several segments by someone as an outputer. Then, well, it will be worse after a long memory, forget the length of the old wooden stick.
Possible minimum output Length
If you have a rough idea, but the code doesn't understand, the best way is to simulate it on paper, or to track and debug other people's code in the previous step on the computer.
First, the possible range of original length:
The shortest length is also longer (or equal) than the longest wooden rod after the cut, and the longest may be cut out by a wooden rod.
In addition, the length of the log rod must be a factor of the total length of the wooden rod.
Explain the meaning of the DFS parameters in the code.
bool DFS(int len, int l, int count, int pos)
- Len: if the length of the log rod is Len, search. Len remains unchanged during the search process.
- L: A len-length wooden rod may be spliced by multiple cut-off wooden bars. L indicates the length of the wood rod being restored. That is to say, one or more wood rods with a total length of len-l can be restored to a wood rod with a length of Len.
- Count: if the total length is sum and the original length is Len, a total of sum/Len wooden sticks are generated. Count records the number of wooden sticks whose length is Len.
- POs: Position of the sticks to be spelled
1 # define local 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <algorithm> 6 using namespace STD; 7 8 struct stick 9 {10 int lenth; 11 bool used; // whether the wooden stick has been used for splicing 12} sticks [55]; 13 14 int N, num, sum; 15 16 bool CMP (stick a, stick B) 17 {18 return (. lenth> B. lenth); 19} 20 21 bool DFS (INT Len, int L, int count, int POS) 22 {23 if (LEN = sum) 24 return true; 25 if (COUNT = num) 26 Return true; // num root sticks have all been spliced to complete 27 for (INT I = Pos; I <n; ++ I) 28 {29 If (sticks [I]. used) 30 continue; 31 if (LEN = sticks [I]. lenth + l) 32 {33 sticks [I]. used = true; 34 if (DFS (Len, 0, Count + 1, 0) // spell a root, spell another 35 return true; 36 sticks [I]. used = false; 37 return false; 38} 39 else if (LEN> sticks [I]. lenth + l) 40 {41 sticks [I]. used = true; // a piece of work can not be completed, first put 42 L + = sticks [I]. lenth; 43 If (DFS (Len, L, Count, I + 1) 44 Return true; 45 sticks [I]. used = false; 46 l-= sticks [I]. lenth; 47 If (L = 0) 48 return false; // This sentence is not particularly understood = _ = !! 49 while (sticks [I]. lenth = sticks [I + 1]. lenth) 50 + + I; // if the two are of the same length and the length does not match, the next one will certainly not work. 51} 52} 53 return false; 54} 55 56 int main (void) 57 {58 # ifdef local59 freopen ("1455in.txt", "r", stdin ); 60 # endif61 62 while (scanf ("% d", & N) 63 {64 sum = 0; 65 for (INT I = 0; I <N; ++ I) 66 {67 scanf ("% d", & sticks [I]. lenth); 68 sticks [I]. used = false; 69 sum + = sticks [I]. lenth; 70} 71 sort (sticks, sticks + N, CMP); 7 2 73 for (INT Len = sticks [0]. lenth; Len <= sum; ++ Len) 74 {75 if (sum % Len! = 0) 76 continue; 77 num = sum/Len; 78 If (DFS (Len, 0, 0, 0) 79 {80 printf ("% d \ n ", len); 81 break; 82} 83} 84} 85 return 0; 86}
Code Jun