nlogn求最长上升子序列
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5;
int n, a[maxn], f[50005], maxx;
int lowbit(int x){return x&(-x);}
int query(int x)
{
int res = 0;
for(int i = x; i > 0; i -= lowbit(i)) res = max(res,f[i]);
return res;
}
void Add(int x,int num)
{
for(int i = x; i <= maxx; i += lowbit(i)) f[i] = max(f[i],num);
}
int main()
{
int ans1 = 0, ans2 = 0;
while(scanf("%d",&a[++n]) != EOF) maxx = max(maxx,a[n]);
n--;
for(int i = n; i >= 1; i--){
int tmp = query(a[i])+1;
Add(a[i],tmp);
ans1 = max(ans1,tmp);
}
memset(f,0,sizeof(f));
for(int i = 1; i <= n; i++){
int tmp = query(a[i]-1)+1;
Add(a[i],tmp);
ans2 = max(ans2,tmp);
}
printf("%d\n%d",ans1,ans2);
return 0;
}