Uva_307
It is equivalent to a review.Poj 1011Pruning is particularly important in this topic.
For this question, the pruning strategy is generally as follows:6Items:
① Sort the length of a wooden rod from large to small, so as to facilitate the selection and operation of the later part.Algorithm.
② When enumerating the length of a log rod, the enumerated range isMaxAndSum/2If no proper length is found in this interval, the final length of the log rod can only beSum.
③ The length of the enumerated log stick can only beSum.
④ In the deep search process, if the length of the current wooden rod is the same as that of the previous one, but the previous one is not selected, then the wooden rod will not be selected.
⑤ In the deep search process, if the current is the first piece of a new wooden rod, but if the longest available wooden rod cannot be used successfully, so you don't have to try the wooden sticks behind it. It must have been a problem in the previous fight process.
⑥ In the deep search process, if the currently available wooden rod can be used to add the last part of a wooden rod, but after it is used, it cannot be used to complete subsequent tasks with the remaining wooden rod, so you don't have to try the wooden sticks. It must have been a problem in the previous fight process.
# Include < Stdio. h >
# Include < String . H >
# Include < Stdlib. h >
Int Sum, n, n, l, [ 100 ], Vis [ 100 ];
Int CMP ( Const Void * _ P, Const Void * _ Q)
{
Int * P = ( Int * ) _ P;
Int * Q = ( Int * ) _ Q;
Return * Q -* P;
}
Int DFS (cur, complete, Len)
{
Int I;
If (Len = L)
{
Complete ++ ;
If (Complete = N)
Return 1 ;
Else
{
For (Cur = 0 ; Vis [cur]; cur ++ );
Vis [cur] = 1 ;
If (DFS (cur + 1 , Complete, a [cur])
Return 1 ;
Vis [cur] = 0 ;
}
}
Else
{
For (I = Cur; I < N; I ++ )
If ( ! Vis [I] && A [I] <= L - Len)
{
If (I ! = 0 && A [I] = A [I - 1 ] &&! Vis [I - 1 ])
Continue ;
Vis [I] = 1 ;
If (DFS (I + 1 , Complete, Len + A [I])
Return 1 ;
Vis [I] = 0 ;
If (A [I] = L - Len)
Return 0 ;
}
}
Return 0 ;
}
Int Main ()
{
Int I, J, K, Max;
While ( 1 )
{
Scanf ( " % D " , & N );
If (N = 0 )
Break ;
Sum = Max = 0 ;
For (I = 0 ; I < N; I ++ )
{
Scanf ( " % D " , & A [I]);
If (A [I] > Max)
Max = A [I];
Sum + = A [I];
}
Qsort (A, N, Sizeof ([ 0 ]), CMP );
Memset (VIS, 0 , Sizeof (VIS ));
For (L = Max; L <= Sum / 2 ; L ++ )
{
If (Sum % L ! = 0 )
Continue ;
N = Sum / L;
If (DFS ( 0 , 0 , 0 ))
Break ;
}
If (L > Sum / 2 )
Printf ( " % D \ n " , Sum );
Else
Printf ( " % D \ n " , L );
}
Return 0 ;
}