zoukankan      html  css  js  c++  java
  • POJ-2533-Longest Ordered Subsequence(最长递增子序列)

    链接:

    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
    ", pos);
    
        return 0;
    }
    
  • 相关阅读:
    Java随笔
    Java随笔
    Java随笔
    CF1271D Portals(反悔贪心)
    CF938D Buy a Ticket(最短路)
    CF1117C Magic Ship(二分)
    HDU6820 Tree(树形dp)
    P2393 美味(主席树+贪心)
    HDU6831 Fragrant numbers(区间dp)
    HDU6832 A Very Easy Graph Problem(生成树)
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11664574.html
Copyright © 2011-2022 走看看