zoukankan      html  css  js  c++  java
  • POJ

    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

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 #include <algorithm>
     5 using namespace std;
     6 int n,a[20010],c[20010],len=0;
     7 int Find(int x)
     8 {
     9     int l=1,r=len,mid;
    10     while(l<=r){
    11         mid=(l+r)>>1;
    12         if(x>c[mid]){ //记忆方法:求上升序列,就表示x更大,那么就是大于
    13             l=mid+1;
    14         }else r=mid-1;
    15     }
    16     return l;
    17 }
    18 int main()
    19 {
    20     scanf("%d",&n);
    21     for(int i=1;i<=n;i++)
    22         scanf("%d",&a[i]);
    23     for(int i=1;i<=n;i++){
    24         int k=Find(a[i]);
    25         c[k]=a[i];
    26         len=max(len,k);
    27     }
    28     printf("%d",len);
    29     return 0;
    30 }
  • 相关阅读:
    ArrayList和HashTable妙用一
    面向过程,面向对象的深入理解一
    jquery中限制部分字段不能输入
    MySql用int存储时间
    android绑定sqlite数据库与程序一起发布
    extjs 4和jquery整合
    jQuery.support 的实现方式
    常用JavaScripts方法
    SVN错误信息大全
    android封装的menu自定义菜单列表
  • 原文地址:https://www.cnblogs.com/wydxry/p/7237876.html
Copyright © 2011-2022 走看看