A
B
C
Question
D: N binary groups. Find the longest sequence group seq. The values of each element in the sequence are greater than those of the previous element.
You can use either the memory-based search or the filtered DP.
Two methods
View code
# Include <cstdio> # Include <Cstring> # Include < Set > # Include < String > # Include <Iostream> # Include <Map> # Include <Vector> # Include <Algorithm> Using Namespace STD; # Define Debug printf ("buuug \ n "); Const Int Maxn = 5010 ; Const Int INF = ~ 0u > 1 ; Int DP [maxn], NXT [maxn]; Struct Node { Int W, H;} p [maxn]; Int N, ans; Int DFS ( Int Pos ){ If (DP [POS]) Return DP [POS]; DP [POS] = 1 ; For ( Int I = 0 ; I <= N; I ++ ){ If (P [I]. W> P [POS]. W & P [I]. h> P [POS]. h ){ If (DFS (I) + 1 > DP [POS]) {NXT [POS] = I; DP [POS] = DP [I] + 1 ;}}} Return DP [POS];} Int Main (){ Int I, J, K; scanf ( " % D " ,& N ); For (I = 0 ; I <= N; I ++ ) Scanf ( " % D " , & P [I]. W ,&P [I]. H); memset (NXT, - 1 , Sizeof (NXT); DFS ( 0 ); Printf ( " % D \ n " , DP [ 0 ]- 1 ); For (I = NXT [ 0 ]; I! =- 1 ; I =NXT [I]) {printf ( " % D " , I);} puts ( "" ); Return 0 ;}
View code
# Include <cstdio> # Include <Cstring> # Include < Set ># Include < String > # Include <Iostream> # Include <Map> # Include <Vector> # Include <Algorithm> Using Namespace STD; # Define Debug printf ("buuug \ n "); Const Int Maxn = 5010 ; Const Int INF = ~ 0u > 1 ; Int DP [maxn], Fa [maxn], ne [maxn]; Int A [maxn]; Struct Node { Int W, h; Int ID;} p [maxn]; Int CMP (node A, Node B ){ If (A. W! = B. W) Return A. W < B. W; Return A. H < B. H ;} Int N, ans; Int Main (){ Int I, J, K, W, H; scanf ( " % D " ,& N); scanf ( " % D " , & W ,& H ); Int TOT = 0 ; For (I = 1 ; I <= N; I ++ ) {Scanf ( " % D " , & P [I]. W ,& P [I]. H ); If (P [I]. W> W & P [I]. h> H) {P [ ++ Tot] = P [I]; P [tot]. ID = I ;}} If (TOT = 0 ) {Puts ( " 0 " ); Return 0 ;} P [ 0 ]. ID = 0 ; DP [ 0 ] = 1 ; Sort (P + 1 , P + tot + 1 , CMP); memset (FA, - 1 , Sizeof (Fa )); Int End; For (I = 1 ; I <= tot; I ++) {DP [I] = 1 ; For (J = 1 ; J <I; j ++ ){ If (P [J]. W <p [I]. W & P [J]. H <p [I]. H & DP [J] + 1 > DP [I]) {DP [I] = DP [J] + 1 ; Fa [I] = J ;}} If (DP [I]> ans) ans = DP [I], end =I;} printf ( " % D \ n " , ANS ); Int TMP = Ans; While (End! =- 1 ) {A [TMP --] = P [end]. ID; End = Fa [end];} For (I = 1 ; I <= ans; I ++) printf ( " % D " , A [I]); // Scanf ("% d", & N ); Return 0 ;} /* 6 1 1900000 900000902400 902400901200 901200903600 903600906000 906000904800 904800 */