Language:DefaultSimplified ChineseSticks
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:120720 |
|
Accepted:27951 |
Description 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 contains blocks of 2 lines. the first line contains the number of sticks parts after cutting, there are at most 64 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 shoshould 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 Central Europe 1995 |
Q: Keep your stick up and ask which of the same sticks are cut down to the minimum possible values.
Very difficult DFS, pruning is very important. I am also reading the question of others, and explained it in the code.
# Include <cstdio> # include <cmath> # include <algorithm> # include <iostream> # include <cstring> using namespace STD; # define n 105int A [n], Len, n; int vis [N], sum; int CMP (int A, int B) {return A> B;} bool DFS (INT POs, int temp, int cut) {If (cut = sum/Len) // return true after successful patchwork; int I; for (I = Pos; I <n; I ++) {If (vis [I] = 0 & temp + A [I] = Len) {vis [I] = 1; if (DFS (0, 0, cut + 1) // remember whether the previous stick may be used, so return true from 0 (the next DFS will find the unused stick); vi S [I] = 0; // I don't know why this place is turned back. If 5 is required, don't use 5. 3 + 2 will be different ???? // It will be wrong if you come back without changing it. If you want to pass by, please kindly advise and return false; // This is because the above 5 is required and it is useless to get 5 (which cannot be pieced together next ), so there is no 2 + 3, because 2 + 3 is more flexible than 5} else if (vis [I] = 0 & temp + A [I] <Len) {vis [I] = 1; if (DFS (I + 1, temp + A [I], cut) return true; vis [I] = 0; if (temp = 0) return false; while (A [I] = A [I + 1]) // if the last time 5 is not obtained, I ++ ;}return false ;}int main () {int I; while (scanf ("% d", & N), n) {sum = 0; for (I = 0; I <n; I ++) {scanf ("% d", & A [I]); sum + = A [I];} Sort (A, A + N, CMP); // descending order for (LEN = A [0]; Len <= sum; len ++) // The stick is not shorter than the longest segment {If (sum % Len) continue; // it must be a multiple of memset (VIS, 0, sizeof (VIS); If (DFS (0, 0) {printf ("% d \ n", Len); break ;}} return 0 ;}
Poj 1011 sticks (Classic DFS)