codevs 3955 最长严格上升子序列(加强版)

题目描述 Description

给一个数组a1, a2 … an,找到最长的上升降子序列ab1 < ab2 < .. < abk , 其中b1 < b2 < ..bk。
输出长度即可。

输入描述 Input Description

第一行,一个整数N。
第二行 ,N个整数(N < = 1000000)

输出描述 Output Description

输出K的极大值,即最长不下降子序列的长度

样例输入 Sample Input

5
9 3 6 2 7

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

n<=1000000

最长上升子序列的加强版,n 有1000000,n 方的 DP 肯定会 TLE ,所以我们使用lower_bound来做

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

const int inf=2147483642;
int dp[1010000];
int a[1010000],b[1010000];
int main()
{
    int n,maxn=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n+1;i++)
        b[i]=inf;
    for(int i=1;i<=n;i++)
    {
        int j=lower_bound(b+1,b+1+n,a[i])-b;
        b[j]=a[i];
        dp[i]=j;
        maxn=max(dp[i],maxn);
    }
    cout<<maxn;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值