zoukankan      html  css  js  c++  java
  • POJ 2533 Longest Ordered Subsequence

    A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence ( ai1ai2, ..., 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.

    Input

    The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000

    Output

    Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.

    Sample Input

    7
    1 7 3 5 9 4 8

    Sample Output

    4

    分析
    最长上升子序列。定义d[i]为以第i个元素为起始的LIS,那么d[i]=max(d[j])+1,(j>i)。初始是d[n]=1。
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<cstring>
    #include <queue>
    #include <vector>
    #include<bitset>
    #include<map>
    #include<deque>
    using namespace std;
    typedef long long LL;
    const int maxn = 1e4+5;
    const int mod = 77200211+233;
    typedef pair<int,int> pii;
    #define X first
    #define Y second
    #define pb push_back
    //#define mp make_pair
    #define ms(a,b) memset(a,b,sizeof(a))
    const int inf = 0x3f3f3f3f;
    #define lson l,m,2*rt
    #define rson m+1,r,2*rt+1
    typedef long long ll;
    #define N 100010
    
    int a[maxn];
    int dp[maxn];
    int main(){
        int n;
        scanf("%d",&n);
    
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        ms(dp,-1);
        for(int i=n;i>=1;i--){
            dp[i]=1;
            int res=0;
            for(int j=i+1;j<=n;j++){
                if(a[i]<a[j]){
                    res=max(res,dp[j]);
                }
            }
            dp[i]+=res;
        }
        int ans=-1;
        for(int i=1;i<=n;i++){
            ans=max(ans,dp[i]);
        }
        cout<<ans<<endl;
        return 0;
    }
     
  • 相关阅读:
    添加绝对路径的链接
    css-------------控制溢出隐藏 换行用省略号表示
    引入公共头部 脚部
    入口图片放在浏览器正中间,点击之后缩小固定在浏览器一侧
    伪类 统一添加样式
    nav 鼠标移入当前高亮显示,其他消失
    17/9/6 bootstrap.css去掉后引发的样式错乱
    JavaScript的常见兼容问题及相关解决方法(chrome/IE/firefox)
    javascript特效实现——当前时间和倒计时效果
    使用DataTables导出excel表格
  • 原文地址:https://www.cnblogs.com/fht-litost/p/8855310.html
Copyright © 2011-2022 走看看