Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, the 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 of this sequence 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.
给出一个长度为N的数字串,找出一个严格上升的数字序列来.

Input

The first line of input contains the length of sequence N (1 <= N <= 1000). The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces.

Output

Output 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

这道题对于现在的我来说已经算是水题了,但是当年我还是看了半个小时才看懂。

f[i]表示到i最长的上升序列

主要的思路就是枚举每个点,然后再与后面的点比较,加入后面的点比他大,长度就+1,即f[j]>f[i],f[j]=f[i]+1;(当然前提是f[i]+1要比原本的f[j]要大)

代码:

 #include<cstdio>
#include<algorithm>
using namespace std;
int s[],n,a[],ans;
int main()
{
    scanf("%d",&n);
    for(int i=;i<=n;i++)
    {
        scanf("%d",&a[i]);
        s[i]=;
    }
    for(int i=;i<=n;i++)
        for(int j=i+;j<=n;j++)
            if(a[i]<a[j]&&s[i]+>=s[j])s[j]=s[i]+,ans=max(ans,s[j]);
    printf("%d",ans);
}

这是O(N^2)的算法,当数据大一些的时候就不行了。

众所皆知,大多数O(N^2)的算法可以用二分优化到O(N log(N))。

没错,就是我们可以用一个数组来存,但是这个数组存的并不是答案,只是当前形成的上升序列,每进来一个数,都用二分查找第一个比他小的数,然后取而代之,如果没有比他小的数,就放到最后面,数组最后的元素个数就是答案。

代码:

 #include<bits/stdc++.h>
using namespace std;
int f[];
int t,m,n;
int main()
{
int m;
cin>>m;
for(int i=;i<=m;i++)
{
int ans=;
cin>>n;
for(int i=;i<=n;i++)
{ cin>>t;
if(i==) f[++ans]=t;
else
{
if(t>f[ans]) f[++ans]=t;
else
{
int x=lower_bound(f+,f+ans,t)-f;
f[x]=t;
}
}
}
cout<<ans<<endl;
}
return ;
}

其实还有树状数组的做法,这里不给出了

最长上升序列(Lis)的更多相关文章

  1. (LIS)最长上升序列(DP+二分优化)

    求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...

  2. XHXJ's LIS HDU - 4352 最长递增序列&数位dp

    代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...

  3. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  4. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  5. 算法面试题 之 最长递增子序列 LIS

    找出最长递增序列 O(NlogN)(不一定连续!) 参考 http://www.felix021.com/blog/read.php?1587%E5%8F%AF%E6%98%AF%E8%BF%9E%E ...

  6. 最长上升子序列LIS(51nod1134)

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

  7. 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  8. 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】

    二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...

  9. 题解 最长上升子序列 LIS

    最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...

  10. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

随机推荐

  1. MT6737 Android N 平台 Audio系统学习----录音到播放录音流程分析

    http://blog.csdn.net/u014310046/article/details/54133688 本文将从主mic录音到播放流程来进行学习mtk audio系统架构.  在AudioF ...

  2. 转载的C#学习笔记

    转载地址:http://www.cnblogs.com/renyanlei/p/4075065.html 最近在一个培训机构里面教授Net知识.每天都会带领学生学习c#知识.我希望把每天学习的笔记记录 ...

  3. SDK Manager中勾选项

    运行SDK Manager 勾选对应版本的SDK,从这里基本可以知道一个Android版本对应着一个版本的API. 其中每个包都有这么几个文件: Documentation for Android S ...

  4. mysql 数据库电脑间迁移

    应用实例: database1(简称DB1)保存在PC1中的MySQL中,需要将DB1迁移到PC2中的MySQL中 环境: win7 MySQL5.7.13 参考: http://stackoverf ...

  5. 关于“C++语言程序设计”书的一个类

    class book{    char* title;    int num_pages;    int cur_page;public:    book(const char* theTitle, ...

  6. [SDOI2012]任务安排

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2726 [算法] 此题与POJ1180非常相似 但是 , 此题中的t值可能为负 , 这 ...

  7. Linux串口通信中一种接收不到数据的问题的解决

    转载来源:嵌入式系统之初学者点滴 (百度空间) 原文 在这篇文章()中,实现了Linux环境下的串口读写操作,程序也运行成功了.但是再进一步测试时发现,如果开机之后直接如上文中所说,分别运行读程序和写 ...

  8. android 怎么实现跑马灯效果

    自定义控件 FocusedTextView, 使android系统误以为它拥有焦点 public class FocusedTextView extends TextView { public Foc ...

  9. Struts文件上传下载

    Struts配置文件: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PU ...

  10. 3-2带命令行参数的Java

    命令行参数: 主方法Main 小括号里面的内容就是命令参数: String[] args class ArgsDemo{ public static void main(String[] args){ ...