网上一堆题解,我写的是N^2优化的那种,nlogn,O(n)的那种能看懂,但是让我自己在赛场写,肯定没戏了
#include <cstdio>
#include <iostream>
#include <ctime>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const int N=1e5+;
int a[N],b[N],n,cnt;
int mx[][N<<];
int now;
void add(int rt,int l,int r,int pos,int t){
if(l==r){
mx[now][rt]=max(mx[now][rt],t);
return;
}
int m=(l+r)>>;
if(pos<=m)add(rt<<,l,m,pos,t);
else add(rt<<|,m+,r,pos,t);
mx[now][rt]=max(mx[now][rt<<],mx[now][rt<<|]);
}
int query(int rt,int l,int r,int x,int y){
if(x<=l&&r<=y)
return mx[now][rt];
int ans=;
int m=(l+r)>>;
if(x<=m)ans=max(ans,query(rt<<,l,m,x,y));
if(y>m)ans=max(ans,query(rt<<|,m+,r,x,y));
return ans;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b++n);
cnt=unique(b+,b++n)-b-;
int pos=lower_bound(b+,b++cnt,a[])-b;
int ans=;
now=;
add(,,cnt,pos,);
now=;
add(,,cnt,pos,);
for(int i=;i<=n;++i){
pos=lower_bound(b+,b++cnt,a[i])-b;
if(pos!=){
now=;
int tmp=query(,,cnt,,pos-)+;
ans=max(ans,tmp);
now=;
add(,,cnt,pos,tmp);
}
if(pos!=cnt){
now=;
int tmp=query(,,cnt,pos+,cnt)+;
ans=max(ans,tmp);
now=;
add(,,cnt,pos,tmp);
}
}
printf("%d\n",ans);
return ;
}