【算法】动态规划
【题解】经典模型:最长上升子序列(n log n)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=50010; int a[maxn],b[maxn],f[maxn],n,m; int find(int x) { int l=1,r=m+1;//m+1是永远不可能被直接比较的,但是必须有 while(l<r) { int mid=(l+r)>>1; if(b[mid]<x)l=mid+1; else r=mid; } return l; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); int p;m=0; int ans=0; for(int i=1;i<=n;i++) { p=find(a[i]); f[i]=p; if(p==m+1)m++,b[m]=a[i];else if(a[i]<b[p])b[p]=a[i]; ans=max(ans,f[i]); } printf("%d",ans); return 0; }