最长递增子序列 (dp)

给出长度为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



nlogn的做法(二分)

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll a[55000];
ll dp[55000];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);
    dp[0]=a[0];
    int l=0,r,mid,len=1;
    for(int i=1;i<n;i++){
        l=0;
        r=len;
        while(l<r){
            mid=(l+r)/2;
            if(dp[mid]<a[i])l=mid+1;
            else r=mid;
        }
        dp[l]=a[i];
        if(l>=len)len++;
    }
    printf("%d\n",len);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值