• 51nod1134 最长递增子序列【动态规划】


    给出长度为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 <= S[i] <= 10^9)

    Output

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

    Input示例

    8
    5
    1
    6
    8
    2
    4
    5
    10

    Output示例

    5

    思路:这题只能用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;
    }
    
  • 相关阅读:
    学习进度(十一)
    学习进度(十)
    人月神话阅读笔记1
    SQL SUM() 函数
    SQL GROUP BY 语句
    SQL HAVING 子句
    SQL UCASE() 函数
    SQL LCASE() 函数
    SQL MID() 函数
    SQL LEN() 函数
  • 原文地址:https://www.cnblogs.com/aerer/p/9930955.html
Copyright © 2020-2023  润新知