O - 最长递增子序列

给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)

例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。

Input

第1行:1个数N,N为序列的长度(2 <= N <= 50000) 
第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= Sii <= 10^9)

Output

输出最长递增子序列的长度。

Sample Input

8
5
1
6
8
2
4
5
10

Sample Output

5
#include<cstring>
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[50010];
int b[50010];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",a+i);
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        int j=lower_bound(b+1,b+ans+1,a[i])-b;//从1-n查找cs数组中小于a[i]的第一个数的次序,记为j(无则为1)
        if(j>ans) b[++ans]=a[i];//若b数组中全小于a[i],则新开一道 
        else b[j]=a[i];//否则将第j个数置为a[i] 
        //for(int i=1;i<=n;i++)cout<<b[i]<<" ";cout<<endl; 
    }
    cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/jack_jxnu/article/details/81068515