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
There are four pruning schemes for this question:
1. The previous stick fails to be installed, and you will not try all the sticks with the same length.
2. If you are replacing the first stick of a whole stick, a failure is returned (replacing the first stick of each whole stick cannot reverse the failure)
3. Ensure that the sequence of the sticks to be installed is from long to short. Then, each time you try a new stick, you will only select a smaller stick than the one recently installed.
4. If you are replacing the last stick of a whole stick, a failure will be returned (replacing the last one of the entire stick cannot reverse the failure)
(1) the code that only adopts the 1 and 2 pruning schemes:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 1000 # define Cls (array, num) memset (array, num, sizeof (array) using namespace STD; int N, stick [maxn], L; bool bused [maxn]; // mark whether a wooden rod has been used bool CMP (int, int B) {return A> B;} bool DFS (INT R, int m) // and R sticks, the remaining length is m {If (r = 0 & M = 0) return true; // If (M = 0) M = L after the task is completed; // after a stick is finished, a new for (INT I = 1; I <= N; I ++) {If (! Bused [I-1] & Stick [I] = stick [I-1]) continue; // pruning 1. The previous wooden rod has the same length as the wooden rod, but it cannot be mounted before. Skip if (bused [I]) continue; If (stick [I] <= m) {bused [I] = true; If (DFS (R-1, M-stick [I]) return true; else {bused [I] = false; if (M = L) return false; // pruning 2. It is futile to change the first stick. No matter you replace the first stick with any other stick, it will fail. Return false }}return false;} int main () {While (1) {Cls (stick, 0); int ntotallen = 0; CIN> N; If (! N) return 0; For (INT I = 1; I <= N; I ++) {CIN> stick [I]; ntotallen + = stick [I];} sort (stick + 1, stick + n + 1, CMP); For (L = stick [N]; L <= ntotallen/2; l ++) // L is the length of each assembled stick {Cls (bused, 0); If (ntotallen % L) continue; // l cannot divide the total length, obviously not, skip if (DFS (n, l) {cout <L <Endl; break ;}} if (L> ntotallen/2) cout <ntotallen <Endl; // if all the previous lengths are not available, it means that after processing, you can only combine them into a whole stick} return 0 ;}
(2) code with four pruning schemes for strong pruning
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 1000 # define Cls (array, num) memset (array, num, sizeof (array) using namespace STD; int N, stick [maxn], L; bool bused [maxn]; // mark whether a wooden rod has been used bool CMP (int, int B) {return A> B;} bool DFS (INT R, int m) // and R sticks, the remaining length is m {If (r = 0 & M = 0) return true; // If (M = 0) M = L after the task is completed; // after a stick is finished, a new for (INT I = 1; I <= N; I ++) {If (! Bused [I-1] & Stick [I] = stick [I-1]) continue; // pruning 1. The previous wooden rod has the same length as the wooden rod, but it cannot be mounted before. Skip if (bused [I]) continue; If (stick [I] <= m) {bused [I] = true; If (DFS (R-1, M-stick [I]) return true; else {bused [I] = false; if (M = L) return false; // pruning 2. It is futile to change the first stick. No matter you replace the first stick with any other stick, it will fail. Return false }}return false;} int main () {While (1) {Cls (stick, 0); int ntotallen = 0; CIN> N; If (! N) return 0; For (INT I = 1; I <= N; I ++) {CIN> stick [I]; ntotallen + = stick [I];} sort (stick + 1, stick + n + 1, CMP); For (L = stick [N]; L <= ntotallen/2; l ++) // L is the length of each assembled stick {Cls (bused, 0); If (ntotallen % L) continue; // l cannot divide the total length, obviously not, skip if (DFS (n, l) {cout <L <Endl; break ;}} if (L> ntotallen/2) cout <ntotallen <Endl; // if all the previous lengths are not available, it means that after processing, you can only combine them into a whole stick} return 0 ;}
The speed difference between the two types of code is too big, surprising!
Zookeeper