给出长度为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\n",LIS(arr,n));
return 0;
}

51Nod - 1134 最长递增子序列【动态规划】的更多相关文章

  1. 51nod 1134 最长递增子序列

    题目链接:51nod 1134 最长递增子序列 #include<cstdio> #include<cstring> #include<algorithm> usi ...

  2. 51nod 1134最长递增子序列

    1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素 ...

  3. LCS 51Nod 1134 最长递增子序列

    给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10.   Input 第1行:1个 ...

  4. LIS 51Nod 1134 最长递增子序列

    给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10.   Input 第1行:1个 ...

  5. 51Nod 1134 最长递增子序列(动态规划O(nlogn))

    #include <iostream> #include <algorithm> #include <stdio.h> #define MAXN 50010 usi ...

  6. 51Nod:1134 最长递增子序列

    动态规划 修改隐藏话题 1134 最长递增子序列  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递 ...

  7. 51 Nod 1134 最长递增子序列 (动态规划基础)

    原题链接:1134 最长递增子序列 题目分析:长度为  的数列  有多达  个子序列,但我们应用动态规划法仍可以很高效地求出最长递增子序列().这里介绍两种方法. 先考虑用下列变量设计动态规划的算法. ...

  8. 51nod 1376 最长递增子序列的数量(线段树)

    51nod 1376 最长递增子序列的数量 数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递 ...

  9. 51nod 1218 最长递增子序列 | 思维题

    51nod 1218 最长递增子序列 题面 给出一个序列,求哪些元素可能在某条最长上升子序列中,哪些元素一定在所有最长上升子序列中. 题解 YJY大嫂教导我们,如果以一个元素结尾的LIS长度 + 以它 ...

随机推荐

  1. UVA 10905

    这题一开始比较错了.两字符串比较应该是 ab和ba两字符串连接起来比较,谁在前面大就排前面. #include <iostream> #include <cstdio> #in ...

  2. MySQL desc作用

    MySQL中默认排序是acs(可省略):从小到大 desc :从大到小,也叫倒序排列.

  3. luogu3390 矩阵快速幂

    矩阵A乘矩阵B是A的第i行向量乘以B的第j列向量的值放在结果矩阵的i行j列.因为矩阵乘法满足结合律,所以它可以与一般的快速幂算法同理使用.注意矩阵在乘的时候取模. #include <cstdi ...

  4. oc46--nonatomic, retain

    // // Person.h #import <Foundation/Foundation.h> #import "Room.h" #import "Car. ...

  5. Codeforces--630A--Again Twenty Five! (水题)

     Again Twenty Five! Time Limit: 500MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u ...

  6. B2002 [Hnoi2010]Bounce 弹飞绵羊 分块

    原来做过,看大家都做这道题都热情高涨,沈爷爷debug这道题4天,作为告诉他这个题的人,我还有点不好意思...我自己也就做了一个小时. 其实这个题思路还好,就是维护每个点的出块次数和跳出块的位置,然后 ...

  7. 3.4 存储简单数据的利器——Preferences

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  8. Appium + python -小程序实例

    from appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionfrom time im ...

  9. UOJ 129/BZOJ 4197 寿司晚宴 状压DP

    //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; ; struct Node ...

  10. BZOJ 2333 左偏树 (写得我人生都崩溃了...)

    思路: 高一神犇 竟然 问我这道题   我光荣地  看着题解(划掉)  写了一下午 QaQ multiset不能erase(一个值)   这样就把等于这个值 的数都erase掉了  (woc我一开始不 ...