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. [Tools] Scroll, Zoom, and Highlight code in a mdx-deck slide presentation with Code Surfer <🏄/>

    If you have a presentation coming up or you just need to present some documentation, then the Code S ...

  2. OpenJudge百炼习题解答(C++)--题3142:球弹跳高度的计算

    题: 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 一球从某一高度落下(整数,单位米),每次落地后反跳回原来高度的一半.再落下. 编程计算气球在第10次落地时,共经过多少米? ...

  3. Hadoop-08-Hive本地独立式安装

    1.安装mysql sudo apt-get install mysql-server mysql-client 2.使用root账户登录mysql数据库,新建存放hive元数据的数据库.如果叫hiv ...

  4. 快看Sample代码,速学Swift语言(2)-基础介绍 快看Sample代码,速学Swift语言(1)-语法速览

    快看Sample代码,速学Swift语言(2)-基础介绍 Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或 ...

  5. 3 微信开发本地代理环境的搭建--实现将内网ip映射到外网

    微信公众号的开发,要搭建网站,并且随时都有可能修改网站内容进行调试,这就需要临时外网能返回本地开发环境搭建的项目进行测试,即内网映射到公网,但是好多开发者没有自己的域名和服务器,这里我们先来搭建一个本 ...

  6. 关于OutOfMemoryError的处理

    转自:http://www.blogjava.net/rosen/archive/2010/05/21/321575.html http://www.blogjava.net/rosen/archiv ...

  7. SpringMVC_架构 --跟海涛学SpringMVC(学习笔记)

    重点: 1.工作流程及实现原理 2.配置及使用方法 3.共同函数 前言 1.2.模型: 1.2.1.此处模型使用JavaBean,可能造成JavaBean组件类很庞大,一般现在项目都是采用三层架构,而 ...

  8. ABAP ODATA接口开发

    1.SE37 创建好 Remote-Enabled函数. 2.SEGW,新建项目 创建项目之后,展开,右键Data Model,Import 函数. 导入函数之后,创建mapping 3./IWFND ...

  9. VMware克隆虚拟机后网络配置

    修复克隆虚拟机文件 # vi /etc/udev/rules.d/70-persistent-net.rules # PCI device 0x8086:0x100f (e1000) SUBSYSTE ...

  10. http_load常见问题及解决方案

    1.错误信息:byte count wrong http_load在处理时会去关注每次访问同一个URL返回结果(即字节数)是否一致,若不一致就会抛出byte count wrong 所以动态页面可以忽 ...