51nod 1134最长上升子序列(O(nlogn)复杂度写法)

这里给出介绍此复杂度算法的链接: 点击打开链接
给出长度为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 <= S[i] <= 10^9)
Output
输出最长递增子序列的长度。
Input示例
8
5
1
6
8
2
4
5
10
Output示例
5

由于帖子里介绍的已经很详细了 所以直接贴出代码

#include<bits/stdc++.h>
using namespace std;
int N,A[50005],B[50005],len=1;
int ef(int x,int y,int k)
{
    while(x<y)
    {   int m=x+((y-x)>>1);
        if(B[m]>k) y=m;
        else if(B[m]<=k) x=m+1;
    }
    return x;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>N;
    for(int i=1;i<=N;i++) cin>>A[i];
    int t=2;
    B[1]=A[1];
    while(t<=N)
    {
        if(A[t]>B[len])
        {
            B[len+1]=A[t];
            len++;
        }
        else if(A[t]<B[len])
        {
            B[ef(1,len,A[t])]=A[t];
        }
        t++;
    }
    cout<<len<<endl;
   return 0;
}

猜你喜欢

转载自blog.csdn.net/whyckck/article/details/79982169