题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)
Description
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
Output
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)的更多相关文章
- poj 2533 Longest Ordered Subsequence 最长递增子序列(LIS)
两种算法 1. O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]
题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...
- POJ 2533 Longest Ordered Subsequence 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- POJ 2533 Longest Ordered Subsequence(裸LIS)
传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- POJ 2533 Longest Ordered Subsequence(LIS模版题)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 47465 Acc ...
- Poj 2533 Longest Ordered Subsequence(LIS)
一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)
传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...
- poj 2533 Longest Ordered Subsequence(LIS)
Description A numeric sequence of ai is ordered ifa1 <a2 < ... < aN. Let the subsequence of ...
随机推荐
- Linux 快照
10个方法助你轻松完成Linux系统恢复 提交 我的留言 加载中 已留言 这也就是为什么系统恢复功能会让人感觉如此神奇.你可以很快地重新回到工作中去,就像什么事情都没有发生一样,也不用去管造成系统故障 ...
- samba.conf (香港中华厨房有限公司实例)
# Sample configuration file for the Samba suite for Debian GNU/Linux. # # This is the main Samba con ...
- Deepin-安装QQ音乐(Windows程序)
打开命令行,输入: sudo apt-get install wine 安装完成后,下载QQ音乐的安装包 然后安装 示例:wine xx.exe 实例:wine QQMusic.exe 安装完成,启动 ...
- 《modern operating system》 chapter 6 DEADLOCKS 笔记
DEADLOCKS Both processes are blocked and will remain so forever. This situation is called a deadlock ...
- 打造极致性能数据库中间件丨LVS+Keepalive+华为云DDM之理论篇
背景说明 华为云分布式数据库中间件(Distributed Database Middleware,简称DDM),专注于解决数据库分布式扩展问题,突破了传统数据库的容量和性能瓶颈,实现海量数据高并发访 ...
- 从CakePHP 1.3升级到2.5
从CakePHP 1.3升级到2.5 摘要:最近把一个CakePHP 1.3的项目升级到了2.x,当然就用最新的版本2.5.3了,结果基本满意.本文记录了升级的过程,包括使用的工具,遇到的问题和相应的 ...
- 2016/3/31 拾遗 php字符串中 转义字符 “ ’‘ ” ’ “” ‘ " \’ ' ' \‘ " " \" '' \ " " 使用
<?php echo $str_string1='甲问:"你在哪里学的PHP?"'; echo "<br />"; echo $str_str ...
- 8核 16g 及时释放内存空间
del 释放 大变量 所在内存空间 GB数据
- 第一个Java程序示例——Hello World!【转】
本文转载自: 跟随世界潮流,第一个Java程序输出“Hell World!”. 通过Eclipse运行程序 启动Eclipse,在菜单中选择“文件 --> 新建 --> Java项目”,弹 ...
- openxml in sql server
OPENXML (Transact-SQL) OPENXML provides a rowset view over an XML document. Because OPENXML is a row ...