Code from someone else's blog
First, a relatively simple version
const int MAXN = 1010;
int n;
int A[MAXN];
int DP[MAXN];
int lis ()
{
memset (DP, 0, sizeof (DP));
int Max;
for (int i = 0; i < n; ++i)
{
Max = 0;
for (int j = 0; J < i; ++j)
{
if (A[i] > A[j])
{
max = max (max, dp[j]);
}
}
Dp[i] = Max + 1;
}
Max = 0;
for (int i = 0; i < n; ++i)
{
if (Dp[i] > Max) max = Dp[i];
}
return Max;
}
And then it's the usual N*LOGN algorithm.
const int MAXN = 1010;
int n;
int A[MAXN];
int DP[MAXN];
int lis ()
{
memset (DP, 0, sizeof (int) *n);
int len = 1;
Dp[0] = a[0];
for (int i = 1; i < n; ++i)
{
int pos = Lower_bound (DP, DP + LEN, A[i])-DP;
Dp[pos] = A[i];
Len = max (len, pos + 1);
}
return len;
}
Above are the maximum ascending subsequence sequences that are to be strictly incremented.
If the title is deformed if the maximum ascending subsequence is to be obtained, I will give a template myself, the giant teaches me
int calc (int sign)
{
fill (dp,dp+maxn,inf);
int ans = 0;
for (int i=0;i<n;i++) {
int index = upper_bound (dp,dp+ans,a[i]*sign)-dp;//This is a non-strict sequence, if strictly required Lower_bound
Dp[index] = a[i]*sign;
ans = max (ans,index+1);
}
return ans;
}
int Lins ()
{
return calc (1);//Ascending
}
int lnds ()
{
return calc (-1);//Descent
}
Refueling ~ ~