O (n^2):
1 /*2 3 Longest strictly ascending sub-sequence4 Dp[i]: Longest strictly ascending subsequence length ending with an integer i5 Dp[i] = max (dp[x]) + 16 Note: x < i && arr[x] < Arr[i]7 8 */9 Ten #define_crt_secure_no_warnings One#include <iostream> A#include <cstdio> -#include <cstdlib> -#include <cmath> the#include <string> -#include <algorithm> -#include <cstring> -#include <Set> +#include <utility> -#include <locale> +#include <ctime> A using namespacestd; at //using Int64 = long long; - Const intINF =0x3f3f3f3f; - Const intMAXN =5010; - - intARR[MAXN]; - intDP[MAXN]; in - voidSolve () to { + } - the intMain () * { $ Panax Notoginseng intLen =0; -CIN >>Len; the for(inti =0; i < Len; ++i) Cin >>Arr[i]; +Fill (DP, DP + LEN,1); A for(inti =0; i < Len; ++i) the { + for(intj =0; J < I; ++j) - { $ if(Arr[j] < arr[i]) dp[i] = max (Dp[i], dp[j] +1); $ } - } -cout << *max_element (DP, DP + len) <<Endl; the - Wuyi #ifdef HOME theCerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl; - #endif Wu return 0; -}
O (NLOGN):
1 /*2 Longest strictly ascending sub-sequence O (NLOGN)3 Dp[i]: The minimum value of the end element in the ascending subsequence of the length of i+1 (not present, that is, INF)4 */5 6#include <iostream>7#include <cstdio>8#include <cstdlib>9#include <cmath>Ten#include <string> One#include <algorithm> A#include <cstring> -#include <Set> -#include <utility> the#include <locale> -#include <limits> -#include <ctime> - using namespacestd; + //using Int64 = long long; - Const intINF = numeric_limits<int>:: Max (); + Const intMAXN =5010; A at intN, ARR[MAXN]; - intDP[MAXN]; - - voidSolve () - { -Fill (DP, DP +N, INF); in for(inti =0; i < N; ++i) - { to*lower_bound (DP, DP + N, arr[i]) =Arr[i]; + } -cout << Lower_bound (DP, DP + N, INF)-DP <<Endl; the } * $ intMain ()Panax Notoginseng { - theCIN >>N; + for(inti =0; i < N; ++i) Cin >>Arr[i]; A Solve (); the + #ifdef HOME -Cerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl; $ #endif $ return 0; -}
Note:
(1) Lower_bound (arr, arr + len, K): Binary search, returns a pointer to the minimum that satisfies the arr[i]>=k.
Detailed reference here: Click
Example reference here: Click
(2) int type maximum value: const int INF = Numeric_limits<int>::max (). Detailed reference here: Click
cannot be set to 0x3f3f3f3f because the test data integer value has exceeded ox3f3f3f3f:
0x3f3f3f3f into 10 binary: 1061109567
Test data:
DP Classic Code[vs] 1576 maximum strict ascending subsequence (O (n^2) and O (Nlogn))