General ideas:
Defines the length of the longest ascending subsequence of dp[i]:= with A[i] as the end element,
The ascending sequence ending with A[i] is:
A sequence that contains only a[i]
An AI-obtained subsequence is appended to the end of the ascending subsequence that satisfies j<i and aj<ai at the end of AJ.
One of them.
This recursive relationship: Dp[i]=max (1,dp[j]+1|j<i&&a[j]<a[i]) can solve the problem in O (n^2) time.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - #pragmaComment (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineLson l,m,rt<<1 to #defineRson m+1,r,rt<<1|1 + #defineE (x) (1 << (x)) - #defineIabs (x) (x) < 0? -(x): (x) the #defineOut (x) printf ("%i64d\n", X) * #defineLowbit (x) (x) & (-X) $ #defineRead () freopen ("A.txt", "R", stdin)Panax Notoginseng #defineWrite () freopen ("B.txt", "w", stdout); - #defineMAXN 100010 the #defineMoD 1000000007 + using namespacestd; A inta[1001],dp[1001]; the intN; + voidSolve () - { $ intres=0; $Memset (DP,0,sizeof(DP)); - for(intI=0; i<n;i++) - { thedp[i]=1; - for(intj=0; j<i;j++)Wuyi { the if(a[i]>A[j]) - { WuDp[i]=max (dp[i],dp[j]+1); - } About } $res=Max (dp[i],res); - } -printf"%d\n", res); - } A intMain () + { the //freopen ("A.txt", "R", stdin); -scanf"%d",&n); $ for(intI=0; i<n;i++) scanf ("%d",&a[i]); the solve (); the return 0; the}
However, if you redefine the recurrence relationship:
dp[i]:= length is the minimum value of the end element in the ascending subsequence of i+1 (not present is INF)
The first dp[i] values are initialized to the INF, and then the elements of the sequence are considered before and after, and for each AJ, if i==0 or dp[i-1]<a[j], there is dp[i]=min (Dp[i],aj) to update and finally find out what makes
The maximum i+1 of dp[i]<inf is the result, and the implementation is also the complexity of O (n^2), but it can be further optimized.
First, the DP sequence is monotonically increasing in addition to the INF, so it is possible to know that for each AJ there is a maximum of one update, and for this update exactly where it is, it does not have to be a two-point search for each iteration.
The complexity is O (NLOGN).
The Low_bound function searches for the smallest pointer to the AI of the ai>=k, using two points from an already sequenced sequence a.
The Upper_bound points to the minimum pointer to the AI that satisfies ai>k.
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> - #pragmaComment (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) - + #definell Long Long - #defineINF 0x7f7f7f7f + #defineLC L,m,rt<<1 A #defineRC M + 1,r,rt<<1|1 at #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 - #defineMin (x, y) (x) < (y)? (x): (y) in #defineMax (x, y) (x) < (y)? (y): (x) - #defineLson l,m,rt<<1 to #defineRson m+1,r,rt<<1|1 + #defineE (x) (1 << (x)) - #defineIabs (x) (x) < 0? -(x): (x) the #defineOut (x) printf ("%i64d\n", X) * #defineLowbit (x) (x) & (-X) $ #defineRead () freopen ("A.txt", "R", stdin)Panax Notoginseng #defineWrite () freopen ("B.txt", "w", stdout); - #defineMAXN 100010 the #defineMoD 1000000007 + using namespacestd; A inta[50001],dp[50001]; the intN; + voidSolve () - { $ for(intI=0; i<n;i++) dp[i]=inf; $ for(intI=0; i<n;i++) - { -*lower_bound (dp,dp+n,a[i]) =A[i]; the } -printf"%d\n", Lower_bound (Dp,dp+n,inf)-DP);Wuyi } the intMain () - { Wu //freopen ("A.txt", "R", stdin); -scanf"%d",&n); About for(intI=0; i<n;i++) scanf ("%d",&a[i]); $ solve (); - return 0; -}
Two methods of solving the longest ascending subsequence