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. assign,copy,strong,weak,nonatomic的具体理解

    例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作:  1 在堆上分配一段内存 ...

  2. Oracle查询正在执行的SQL语句

    查看 Oracle 正在执行的 sql 语句以及发起的用户 SELECT b.sid oracleID, b.username 用户名, b.serial#, paddr, sql_text 正在执行 ...

  3. Python序列——字符串

    字符串 1 string模块预定义字符串 2 普通字符串与Unicode字符串 3 只适用于字符串的操作 4 原始字符串 5 Unicode字符串操作符 内建函数 1 标准类型函数与序列操作函数 2 ...

  4. windows64位安装mysql-5.7.12,图文

    linux下安装mysql教程一大片,我就不说了,再此说下windows 下如何安装这个5.7版本,并且有些坑已踩! 一:进入mysql下载地址:http://www.mysql.com/downlo ...

  5. POJ 2823 Sliding Window (滑动窗口的最值问题 )

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 41264   Accepted: 12229 ...

  6. ReentrantReadWriteLock读写锁实现分析

    排他锁在同一时刻只允许一个线程进行访问,而读写锁在同一时刻允许多个读线程访问,但是在写线程访问时,所有的读线程和其他的写线程均被阻塞.读写锁内部维护了一对锁,一个读锁和一个写锁,通过分离读锁和写锁,使 ...

  7. 让tomcat启动时,自动加载你的项目

    在tomcat-->conf-->serve.xml文件最后加上 <Context path="/atest" docBase="E:\Workspac ...

  8. Duplicate files copied in APK META-INF/DEPENDENCIES

    在app的目录下找到build.gradle 这个文件,在android标签的最后面加入以下信息: packagingOptions { exclude 'META-INF/DEPENDENCIES' ...

  9. QQ通信原理

    转载自http://blog.csdn.net/li_xiao_ming/article/details/8106857 下面有4个基本的问答: 问题一:为什么只要可以连上互联网的计算机都可以用QQ相 ...

  10. java虚拟机内存溢出和泄漏实例

    测试参数设置: 1.循环调用new A()实现堆溢出,java.lang.OutOfMemoryError: Java heap space, 虚拟机参数:-Xms1M -Xmx1M -XX:+Hea ...