首页 > 其他 > 详细

POJ-2533-Longest Ordered Subsequence(最长递增子序列)

时间:2019-10-13 09:55:45      阅读:80      评论:0      收藏:0      [点我收藏+]

链接:

https://vjudge.net/problem/POJ-2533

题意:

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).

Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.

思路:

维护一个数组, 大于的直接加上去, 小的去更新内部.
nlogn

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 2e3+10;

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

int main()
{
    scanf("%d", &n);
    for (int i = 1;i <= n;i++)
        scanf("%d", &a[i]);
    int pos = 0;
    Dp[0] = -1;
    for (int i = 1;i <= n;i++)
    {
        if (a[i] > Dp[pos])
            Dp[++pos] = a[i];
        else
        {
            int p = lower_bound(Dp+1, Dp+1+pos, a[i])-Dp;
            Dp[p] = a[i];
        }
    }
    printf("%d\n", pos);

    return 0;
}

POJ-2533-Longest Ordered Subsequence(最长递增子序列)

原文:https://www.cnblogs.com/YDDDD/p/11664574.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!