Description

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence ( ai1ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, 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 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.

Input

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

Output

Output file 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
解题思路:典型dp:最长上升子序列问题。有两种解法,一种O(n^2),另一种是O(nlogn)。相关详细的讲解:LIS总结
AC代码一:朴素O(n^2)算法,数据小直接暴力。
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=;
int n,res,dp[maxn],a[maxn];
int main(){
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));res=;
for(int i=;i<n;++i)scanf("%d",&a[i]),dp[i]=;//每个自身都是一个长度为1的子序列
for(int i=;i<n;++i){
for(int j=;j<i;++j)
if(a[j]<a[i])dp[i]=max(dp[i],dp[j]+);//只包含i本身长度为1的子序列
res=max(res,dp[i]);
}
printf("%d\n",res);
}
return ;
}

AC代码二:进一步优化,采用二分法每次更新最小序列,最终最小序列的长度(其最终的序列不一定是正确的LIS,只是某个过程中有这个最长的序列,其长度就是最终<INF的元素个数)就是最长上升子序列长度。时间复杂度是O(nlogn)。dp[i]:长度为i+1的上升子序列中末尾元素的最小值(不存在的话就是INF),此处dp是针对相同长度下最小的末尾元素进行求解,角度转换十分巧妙。

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=;
const int INF=0x3f3f3f3f;
int n,res,x,dp[maxn];
int main(){
while(~scanf("%d",&n)){
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;++i){
scanf("%d",&x);
*lower_bound(dp,dp+n,x)=x;//更新最小序列
}
printf("%d\n",lower_bound(dp,dp+n,INF)-dp);
}
return ;
}

题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)的更多相关文章

  1. poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)

    两种算法 1.  O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...

  2. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  3. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  4. POJ 2533 Longest Ordered Subsequence 最长递增序列

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

  5. POJ 2533 Longest Ordered Subsequence(裸LIS)

    传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 6 ...

  6. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  7. Poj 2533 Longest Ordered Subsequence(LIS)

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

  8. POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)

    传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...

  9. poj 2533 Longest Ordered Subsequence(LIS)

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

随机推荐

  1. Linux 快照

    10个方法助你轻松完成Linux系统恢复 提交 我的留言 加载中 已留言 这也就是为什么系统恢复功能会让人感觉如此神奇.你可以很快地重新回到工作中去,就像什么事情都没有发生一样,也不用去管造成系统故障 ...

  2. samba.conf (香港中华厨房有限公司实例)

    # Sample configuration file for the Samba suite for Debian GNU/Linux. # # This is the main Samba con ...

  3. Deepin-安装QQ音乐(Windows程序)

    打开命令行,输入: sudo apt-get install wine 安装完成后,下载QQ音乐的安装包 然后安装 示例:wine xx.exe 实例:wine QQMusic.exe 安装完成,启动 ...

  4. 《modern operating system》 chapter 6 DEADLOCKS 笔记

    DEADLOCKS Both processes are blocked and will remain so forever. This situation is called a deadlock ...

  5. 打造极致性能数据库中间件丨LVS+Keepalive+华为云DDM之理论篇

    背景说明 华为云分布式数据库中间件(Distributed Database Middleware,简称DDM),专注于解决数据库分布式扩展问题,突破了传统数据库的容量和性能瓶颈,实现海量数据高并发访 ...

  6. 从CakePHP 1.3升级到2.5

    从CakePHP 1.3升级到2.5 摘要:最近把一个CakePHP 1.3的项目升级到了2.x,当然就用最新的版本2.5.3了,结果基本满意.本文记录了升级的过程,包括使用的工具,遇到的问题和相应的 ...

  7. 2016/3/31 拾遗 php字符串中 转义字符 “ ’‘ ” ’ “” ‘ " \’ ' ' \‘ " " \" '' \ " " 使用

    <?php echo $str_string1='甲问:"你在哪里学的PHP?"'; echo "<br />"; echo $str_str ...

  8. 8核 16g 及时释放内存空间

    del  释放 大变量 所在内存空间 GB数据

  9. 第一个Java程序示例——Hello World!【转】

    本文转载自: 跟随世界潮流,第一个Java程序输出“Hell World!”. 通过Eclipse运行程序 启动Eclipse,在菜单中选择“文件 --> 新建 --> Java项目”,弹 ...

  10. openxml in sql server

    OPENXML (Transact-SQL) OPENXML provides a rowset view over an XML document. Because OPENXML is a row ...