[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

为了方便大家调试,数据名称已被修改——THREE

朴素做法O(n^2)

注意的是,答案是f的最大值。

//Writer:GhostCai && His Yellow Duck

#include<iostream>
#define MAXN 1000005
using namespace std;

int f[MAXN],a[MAXN];
int n;

int main(){
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++){
        f[i]=1;
        for(int j=1;j<i;j++){
            if(a[j]<a[i]&&f[j]+1>f[i]) f[i]=f[j]+1; 
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++) ans=max(ans,f[i]);
    cout<<ans<<endl;
//  cout<<f[n];
    return 0;
}

本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247509.html

原文地址:https://www.cnblogs.com/ghostcai/p/9247509.html