1134 最长递增子序列(51nod) 解题报告

1134 最长递增子序列 

基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题

 收藏

 关注

给出长度为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<cstdio>
#include<iostream>
#include<cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int maxn=5e4+7;
int dp[maxn];
int a[maxn];

int main()
{
	int n;
	while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        memset(dp,0,sizeof(dp));
        int len=0;
        dp[0]=a[0];
        for(int i=1;i<n;i++)
        {
            if(a[i]>dp[len])
                dp[++len]=a[i];
            else
            {
                int tem=a[i];
                int l=0,r=len;
                while(l<r)
                {
                    int mid=l+r>>1;
                    if(dp[mid]<tem) l=mid+1;
                    else r=mid;
                }
                dp[l]=a[i];

            }
        }
        printf("%d\n",len+1);
    }
	return 0;

}

猜你喜欢

转载自blog.csdn.net/weixin_42373330/article/details/82152771