The dynamic planning method is used to calculate the longest incrementing subsequence from the start to the end. We need to open two arrays.
Code
# Include < Stdio. h >
Int Main ()
{
Int I, j, t, n, m;
Int A [ 905 ], B [ 905 ];
Scanf ( " % D " , & T );
While (T -- ){
Scanf ( " % D " , & N, & M );
For (I = 0 ; I < N; I ++ )
Scanf ( " % D " , & A [I]), B [I] = 1 ;
For (I = 1 ; I < N; I ++ ){
For (J = 0 ; J < I; j ++ ){
If (A [J] < A [I] && B [J] + 1 > B [I])
B [I] = B [J] + 1 ;
}
}
// Printf ("% d \ n", B [n-1]);
If (B [n - 1 ] > = M)
Puts ( " Good " );
Else
Puts ( " Bad " );
}
Return 0 ;
}
Until last night, the team members discussed with me, and I found that the greedy algorithm is used to find the longest incrementing subsequence. the space and time complexity are smaller than the dynamic planning method.
Method: J = 0; I starts from 0 to n-1, and each time we go to a [I]> A [J], let a [J + 1] = A [I], J + = 1; if a [I] <= A [J], replace the first number smaller than a [I] from a [I-1] in a [0] with a [I].
By the end of the loop, the value of J ++ is the result of obtaining the longest ascending subsequence.
Code
# Include < Stdio. h >
Int Main ()
{
Int I, J, K, t, n, m;
Int A [ 905 ];
Scanf ( " % D " , & T );
While (T -- ){
Scanf ( " % D " , & N, & M );
For (I = 0 ; I < N; I ++ )
Scanf ( " % D " , & A [I]);
J = 0 ;
For (I = 1 ; I < N; I ++ ){
If (A [I] > A [J])
A [ ++ J] = A [I];
Else
For (K = 0 ; K < I; k ++ )
If (A [k] > = A [I]) {
A [k] = A [I];
Break ;
}
}
If ( ++ J > = M)
Puts ( " Good " );
Else
Puts ( " Bad " );
}
Return 0 ;
}
AboveCodeThe corresponding topic is the high jump snail on zzuli.