zoukankan      html  css  js  c++  java
  • 51Nod

    给出长度为N的数组,找出这个数组的最长递增子序列。(递增子序列是指,子序列的元素是递增的)

    例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10。

    Input

    第1行:1个数N,N为序列的长度(2 <= N <= 50000) 
    第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= Sii <= 10^9)

    Output

    输出最长递增子序列的长度。

    Sample Input

    8
    5
    1
    6
    8
    2
    4
    5
    10

    Sample Output

    5

    思路:显而易见,这道题要用动态规划和二分来写,复杂度O(nlogn),n2会超时。

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int arr[50005];
    int BinarySearch(int *arr,int value,int len)
    {
        int begin =0,end=len-1;
        while(begin<=end)
        {
            int mid=begin+(end-begin)/2 ;
            if(arr[mid]==value)
                return mid;
            else if(arr[mid]>value)
                end=mid-1;
            else
                begin=mid+1;
        }
        return begin;
    }
    
    int LIS(int *arr,int len)
    {
        int a[len],n=1;
        a[0]=arr[0];
        for(int i=1;i<len;++i)
        {
            if(arr[i] > a[n-1])
            {
                a[n]=arr[i];
                ++n;
            }
            else
            {
                int pos = BinarySearch(a,arr[i],n);
                a[pos]=arr[i];
            }
        }
        return n;
    }
    
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;++i)
            scanf("%d",&arr[i]);
        printf("%d
    ",LIS(arr,n));
        return 0;
    }
  • 相关阅读:
    ExtJs中ComboBox使用之技巧
    Ethernet帧结构
    Sqlite修改表
    Solr系列:Linux下部署Solr
    Ethernet帧的接收流程
    使用SQLite作为Quartz.Net的Job Store
    Quartz.Net的一个Bug
    获取程序集的Public key token
    SQLite数据类型
    shell取出多列文件中某一列重复的数据
  • 原文地址:https://www.cnblogs.com/aerer/p/9930988.html
Copyright © 2011-2022 走看看