题目
思路
这道题是一道经典DP题
但可以用树状数组优化。
貌似不可以处理有相同数的情况(别问我为什么……)
代码
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
long long n,ans,c[2000020];
long long f[2000020];
pair <long long,long long> a[2000020];
void insert(long long x,long long y)
{
while(x<=200020)
{
c[x]=max(c[x],y);
x+=(x&-x);
}
}
long long query(long long x)
{
long long maxn=0;
while(x)
{
maxn=max(maxn,c[x]);
x-=(x&-x);
}
return maxn;
}
int main()
{
scanf("%lld",&n);
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i].first);
a[i].second=i;
}
sort(a+1,a+1+n);
for(int i=1; i<=n; i++)
{
f[i]=query(a[i].second)+1;
insert(a[i].second,f[i]);
}
for(int i=1; i<=n; i++)
ans=max(ans,f[i]);
printf("%lld",ans);
return 0;
}