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

    LIS:

    维护一个单调的队列
    新的元素,如果大于队尾元素,即插入队尾
    否则二分查找比它大的最小元素,替换掉
    最后队列长度即为LIS的解
     
     
    1 3 7 5 9 4 8
    1
    1 3
    1 3 7
    1 3 5
    1 3 5 9
    1 3 4 9
    1 3 4 8
     
                                                                                 Longest Ordered Subsequence
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 26637   Accepted: 11611

    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

    71 7 3 5 9 4 8

    Sample Output

    4

    Source

    Northeastern Europe 2002, Far-Eastern Subregion
     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 int a[1111];
     6 int b[1111];
     7 
     8 int BinarySearch(int* a,int l,int r,int x)
     9 {
    10     int L=l,R=r;
    11     while(L<R)
    12     {
    13         int M=(R+L)/2;
    14         if(a[M]<=x) L=M+1;
    15         else  R=M;
    16     }
    17     return R;
    18 }
    19 
    20 int main()
    21 {
    22     int n;
    23     cin>>n;
    24     for(int i=0;i<n;i++)
    25         cin>>a[i];
    26     int cur=0; b[0]=-999999929;
    27 
    28     for(int i=0;i<n;i++)
    29     {
    30         if(a[i]>b[cur])
    31         {
    32             b[++cur]=a[i];
    33         }
    34         else if(a[i]<b[cur])
    35         {
    36             b[BinarySearch(b,1,cur,a[i])]=a[i];
    37         }
    38     }
    39     cout<<cur<<endl;
    40 
    41     return 0;
    42 }
  • 相关阅读:
    MFC中L, _T(),TEXT,_TEXT区别以及含义
    Qt5完美解决 界面显示中文乱码
    TCP三次握手四次挥手详解
    TCP 长连接与短连接的区别
    Servlet 生命周期、工作原理
    Java反射的三种实现方式
    apache httpclient 4.5 兼容 http https
    mysql千万级大数据SQL查询优化
    Java String字符串补0或空格
    mysql存储过程
  • 原文地址:https://www.cnblogs.com/CKboss/p/3086809.html
Copyright © 2011-2022 走看看