Subsequence with longest monotonic increasing
Question: design an O (N ^ 2) complexity algorithm to find the longest monotonic increasing subsequence of a sequence composed of N numbers.
Import Java. util. extends; public class main {public static void main (string [] ARGs) {extends = new loads (system. in); While (partial. hasnext () {int n = seconds. nextint (); int [] Nums = new int [n + 1]; // number of series data int [] sub_len = new int [n + 1]; // sub_len [I] indicates the length of the longest ascending sequence from I to N int [] sub_index = new int [n + 1]; // sub_index [I] indicates the subscript for (INT I = 1; I <= N; I ++) {Nums [I] = numeric. nextint (); sub_l En [I] = 1; sub_index [I] = 0;} For (INT I = n-1; I> = 1; I --) {int max = 0, P = 0; // temporary variable for (Int J = I + 1; j <= N; j ++) {If (Nums [I] <Nums [J] & sub_len [J]> MAX) {max = sub_len [J]; P = J ;}if (P! = 0) {sub_len [I] = sub_len [p] + 1; sub_index [I] = P ;}} int max = 0, P = 0; for (INT I = 1; I <= N; I ++) {If (sub_len [I]> MAX) {max = sub_len [I]; P = I ;}} system. out. println ("maximum length:" + max); system. out. print ("Longest ascending sequence:"); While (P! = 0) {system. out. print (Nums [p] + ""); P = sub_index [p];} system. out. println ();} bytes. close ();}}
Subsequence with longest monotonic increasing