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

    Longest Ordered Subsequence
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 35037   Accepted: 15372

    Description

    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
    大致题意:给出一个数列,求最大不下降子序列的长度。

    思路:最优结构:dp[i]以第i个元素为子序列最后一个元素的LIS的长度。

    状态转移方程:dp[1]=1;dp[i]=max(dp[j] | j<i && m[j] < m[i] ) +1 ;i>1

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 const int inf = 10001;
     5 const int MAX = 1005;
     6 int binary_search(int s[],int digit,int length)
     7 {
     8     int left=0,right=length,mid;
     9     while(right!=left)
    10     {
    11         mid=(left+right)/2;
    12         if(digit==s[mid])
    13             return mid;
    14         else if(digit<s[mid])
    15             right=mid;
    16         else
    17             left=mid+1;
    18     }
    19     return left;
    20 }
    21 int main()
    22 {
    23     int n,i,j;
    24     while(~scanf("%d",&n))
    25     {
    26         int a[MAX];
    27         int s[MAX];
    28         for(i=1;i<=n;i++)
    29             scanf("%d",&a[i]);
    30         s[0]=-1;
    31         int len=1;
    32         for(i=1;i<=n;i++)
    33         {
    34             s[len]=inf;
    35             j=binary_search(s,a[i],len);
    36             if(j==len)
    37                 len++;
    38             s[j]=a[i];
    39         }
    40         printf("%d
    ",len-1);
    41     }
    42     return 0;
    43 }
    
    
    
     
  • 相关阅读:
    PHP线程安全和非线程安全有什么区别
    SHOW SLAVE STATUS 详解
    linux 2>&1
    crontab 例行性排程
    clang-format 数组初始化,多行模式
    windows 10 1909 无法启用 .NET Framework 解决
    SysCtlDelay 实现延时
    wpa_supplicant 检测错误密码
    swig python dynamic module does not define init function
    讯飞错误码10116
  • 原文地址:https://www.cnblogs.com/caterpillarofharvard/p/4226329.html
Copyright © 2011-2022 走看看