First, a n^2 algorithm:
1#include <iostream>2 using namespacestd;3 4 Const intN = +;5 intA[n];6 intG[n];7 8 intMain ()9 {Ten intN; One while(Cin >>N) A { - for(inti =0; I < n; i++ ) - { theCIN >>A[i]; -G[i] =1; - } - intAns = g[0]; + for(inti =1; I < n; i++ ) - { + for(intj =0; J < I; J + + ) A { at if(A[j] >= a[i])Continue; - if(G[j] +1> G[i]) g[i] = G[j] +1; - } - if(G[i] > ans) ans =G[i]; - } -cout << ans <<Endl; in } - return 0; to}
And then it's NLOGN:
1#include <algorithm>2#include <iostream>3 using namespacestd;4 5 Const intINF =1<< in;6 Const intN = +;7 intS[n];8 inttop;9 Ten intMain () One { A intN; - while(Cin >>N) - { thetop =0; -s[top++] =INF; - for(inti =0; I < n; i++ ) - { + inttmp; -CIN >>tmp; + if(tmp > S[top-1] ) A { ats[top++] =tmp; - } - Else - { - intpos = Upper_bound (s, S + top, TMP)-s; -S[pos] =tmp; in } - } tocout << Top <<Endl; + } - return 0; the}
Nlogn solution of longest ascending subsequence sequence POJ 2533