最长不上升子序列和最长不下降子序列
C:勤奋的杨老师
AC代码:
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<ctype.h>
#include<string>
#include<cmath>
#include<bitset>
#define caseT int _T;scanf("%d",&_T);for(int q=1; q<=_T; ++q)
#define mm1(a) memset((a),-1,sizeof((a)))
#define mm0(a) memset((a),0,sizeof((a)))
#define mmx(a) memset((a),0x3f,sizeof((a)))
#define ka getchar()
#define lowbit(x) (x)&(-(x))
#define fuck(x) printf("*%d\n",(x))
#define test printf("***\n")
#define iis std::ios::sync_with_stdio(false)
#define MP make_pair
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
const int N = 500005;
const int M = 4100005;
const int X = 999983;
const int INF = 1e9;
const double eps = 1e-8;
const int mod = 1e9 + 7;
int n;
int ar[N];
int t[N];
int dp1[N],dp2[N];
int main(){
#ifdef DEBUG
freopen("D:in.in", "r", stdin);
freopen("D:out.out", "w", stdout);
#endif
while(~scanf("%d",&n)){
for(int i=1;i<=n;++i){
scanf("%d",&ar[i]);
}
int k=1;
mm0(t);
mm0(dp1);
mm0(dp2);
t[1]=ar[1];
dp1[1]=1;
for(int i=2;i<=n;++i){
if(ar[i]>=t[k]){
t[++k]=ar[i];
dp1[i]=k;
}else{
int p=upper_bound(t+1,t+k+1,ar[i])-t;
t[p]=ar[i];
dp1[i]=p;
}
}
mm0(t);
k=1;
t[1]=ar[n];
dp2[n]=1;
for(int i=n-1;i>=1;--i){
if(ar[i]>=t[k]){
t[++k]=ar[i];
dp2[i]=k;
}else{
int p=upper_bound(t+1,t+k+1,ar[i])-t;
t[p]=ar[i];
dp2[i]=p;
}
}
int ans=0;
for(int i=1;i<=n;++i){
ans=max(ans,dp1[i]+dp2[i]-1);
}
printf("%d\n", ans);
}
#ifdef DEBUG
fclose(stdout);
fclose(stdin);
#endif
return 0;
}