最长上升子序列

Posted Ed_Sheeran

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了最长上升子序列相关的知识,希望对你有一定的参考价值。

O(nlogn)算法:不能得到序列本身

贪心思想,同样是长度为tot的最长上升子序列,结尾的数字越小对后面的选择越有利,所以若以a[i]结尾的最长上升子序列长为tot, a[j]结尾也是这样,且j>i&&a[i]>a[j],就用a[j]替换a[i],正确性自证

#include <bits/stdc++.h>

using namespace std;
int d[1005];
int main()
{
    int n,u, top=0;
    scanf("%d%d",&n,&u);

    d[0] = d[++top] = u;
    for(int i = 2; i <= n; i++){
        int u;
        scanf("%d",&u);
        if(u > d[top])d[++top] = u;
        else{
            int pos = lower_bound(d + 1, d + 1 + top, u) - d;
            d[pos] = u;
        }
    }
    printf("%d\n",top);
    return 0;
}

 

以上是关于最长上升子序列的主要内容,如果未能解决你的问题,请参考以下文章

刷题之最长公共/上升子序列问题

每日算法-05(最长上升子序列)

最长上升子序列

LIS LCS 最长上升子序列 最长公共子序列 ...

浅谈最长不下降子序列与最长上升子序列

最长上升子序列