Problem description: There are several original sticks, each of which is split into several. Now we need to restore them. Find such a subset so that the sum of each subset is equal to (Plen) and the minimum value is
Algorithm: Sort, traverse all possible Plen
Pruning: after sorting, a [0] a [1] A [2] ...... A [n-1], if you want to match a number V starting from a [I] (excluding I), if I + 1, I + 2 ...... if the n-1 match fails, I + 2, I + 3 .... n-1 does not match, so the AC version is obtained after modifying the Tle version.
TLE:
# Include <iostream> # include <algorithm> Using Namespace STD; # define Max 64 Int Sticks [Max]; Bool Used [Max]; Int Sticknum, Plen, N; Bool Compare ( Int A,Int B ){ Return A> B ;} // Match from beginindex. The next step is to match the stick of matchlen. hasmatch stick has been matched before. Bool DFS ( Int Beginindex, Int Matchlen, Int Hasmatch ){ // Printf ("% 4D % 4D % 4D \ n", beginindex, matchlen, hasmatch ); If (Matchlen = 0) {hasmatch ++; // Printf ("hasmatch = % d, sticknum = % d \ n", hasmatch, sticknum ); If (Hasmatch = sticknum ){// Printf ("hasmatch = sticknum \ n "); Return True ;} For (Beginindex = 0; used [beginindex]; beginindex ++ ); // Used [beginindex] = true; // Printf ("match = % d begin = % d \ n", hasmatch, beginindex ); If (DFS (beginindex, Plen, hasmatch )) Return True ; // Used [beginindex] = false; Return False ;} Else {If (Beginindex> N-1) Return False ; For ( Int I = beginindex; I <n; I ++ ){ If (Used [I]) Continue ; If (Sticks [I]> matchlen) Continue ; If (I> 0 & sticks [I] = sticks [I-1] &! Used [I-1]) Continue ; Used [I] = True ;If (DFS (I + 1, matchlen-sticks [I], hasmatch )) Return True ; Used [I] = False ;}} // Printf ("end % 4D % 4D % 4D false \ n", beginindex, matchlen, hasmatch ); Return False ;} Int Main ( Int Argc, Char * Argv []) { Int Sum = 0; Int I; While (Scanf (" % D ", & N) {sum = 0; For (I = 0; I <n; I ++) {scanf (" % D ", & Sticks [I]); sum + = sticks [I];} Bool OK = False ; Sort (sticks, sticks + N, compare ); For (Plen = sticks [0]; Plen <= sum/2; Plen ++ ){ If (Sum % Plen = 0 ){ // Used [0] = true; Sticknum = sum/Plen; If (DFS (0, Plen, 0) {OK = True ; Break ;} // Used [0] = false; }} If (OK) {printf (" % D \ n ", Plen );} Else {Printf (" % D \ n ", Sum);} memset (used, False , Sizeof (Used ));} Return 0 ;}
AC:
# Include <iostream> # include <algorithm> Using Namespace STD; # define Max 64 Int Sticks [Max];Bool Used [Max]; Int Sticknum, Plen, N; Bool Compare ( Int A, Int B ){ Return A> B ;} // Match from beginindex. The next step is to match the stick of matchlen. hasmatch stick has been matched before. Bool DFS ( Int Beginindex, Int Matchlen, Int Hasmatch ){// Printf ("% 4D % 4D % 4D \ n", beginindex, matchlen, hasmatch ); If (Matchlen = 0) {hasmatch ++; // Printf ("hasmatch = % d, sticknum = % d \ n", hasmatch, sticknum ); If (Hasmatch = sticknum ){ // Printf ("hasmatch = sticknum \ n "); Return True ;} For (Beginindex = 0; used [beginindex]; beginindex ++ ); // Printf ("match = % d begin = % d \ n", hasmatch, beginindex ); Used [beginindex] =True;If(DFS (beginindex + 1, Plen-sticks [beginindex], hasmatch ))Return True; Used [beginindex] =False; Return False ;} Else { If (Beginindex> N-1) Return False ; For ( Int I = beginindex; I <n; I ++ ){ If (Used [I])Continue ; If (Sticks [I]> matchlen) Continue ; If (I> 0 & sticks [I] = sticks [I-1] &! Used [I-1]) Continue ; Used [I] = True ; If (DFS (I + 1, matchlen-sticks [I], hasmatch )) Return True ; Used [I] = False ;}} // Printf ("end % 4D % 4D % 4D false \ n", beginindex, matchlen, hasmatch ); Return False ;} Int Main ( Int Argc, Char * Argv []) { // Freopen ("I: // in.txt", "r", stdin ); Int Sum = 0; Int I; While (Scanf (" % D ", & N) {sum = 0; For (I = 0; I <n; I ++) {scanf (" % D ", & Sticks [I]); sum + = sticks [I];} Bool OK =False ; Sort (sticks, sticks + N, compare ); For (Plen = sticks [0]; Plen <= sum/2; Plen ++ ){ If (Sum % Plen = 0 ){ Used [0] =True; Sticknum = sum/Plen;If(DFS (0, Plen-sticks [0], 0) {OK =True;Break;} Used [0] =False; }} If (OK) {printf (" % D \ n ", Plen );} Else {Printf (" % D \ n ", Sum);} memset (used, False ,Sizeof (Used ));} Return 0 ;}