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 }
  • 相关阅读:
    IDEA启动报错 NoClassDefFound
    IDEA手动导入jar包到maven本地库
    springboot FilterRegistrationBean 拦截器的使用
    springboot多模块controller访问的问题
    @Slf4j -- lombok.extern.slf4j.Slf4j;
    @mapper注解
    lombok
    Lodop打印小票
    spring security登录认证流程解析
    使用selenium实现简单网络爬虫抓取MM图片
  • 原文地址:https://www.cnblogs.com/wydxry/p/7237876.html
Copyright © 2011-2022 走看看