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. HTML的DIV如何实现水平居中

    内部的DIV必须有下面两行代码即可 text-align:center; margin:0 auto;   在IE6中同样可以

  2. composer-安装插件包

    上一步完成后,选定国内镜像地址,以为下载插件包做准备 https://pkg.phpcomposer.com/ 安装完componser后使用下面这条命令即可(设置国内镜像地址): composer ...

  3. 【手记】小心在where中使用NEWID()的大坑 【手记】解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题 【C#】组件分享:FormDragger窗体拖拽器 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    [手记]小心在where中使用NEWID()的大坑 这个表达式: ABS(CHECKSUM(NEWID())) % 3 --把GUID弄成正整数,然后取模 是随机返回0.1.2这三个数,不可能返回其它 ...

  4. iOS开发人员:事实上你还有非常多东西须要学

    iOS 新特性总结(since iOS6) iOS 6 1.废除viewDidUnLoad 收到内存警告须要到didReceiveMemoryWarning中处理 [小技巧] -(void)didRe ...

  5. Android开发之自己定义Spinner样式的效果实现(源码实现)

    android系统自带的Spinner样式是远远满足不了我们实际开发过程中对Spinner UI风格的要求,因此我们肯定须要为了切合整个应用的风格,改动我们的Spinner样式.系统给我们提供了两种常 ...

  6. 我的kindle书单

    刚刚入手kindle,希望能够持续阅读,不断进步. 列下书单,记录我的阅读足迹,更希望园友若有好书多多推荐,互相交流. # keep updating ... 我的kindle书单 book name ...

  7. 对ASP.NET MVC 的路由一点理解

    这个东西,真搞不懂.看了网上的教程和文章,也不懂(也不清楚写那些文章的人自己是否真的懂).只好靠自己一顿乱摸索. 好比说,下面这个路由: //路由1 config.Routes.MapHttpRout ...

  8. 安全相关的head头

    与安全相关的head头包括 参考网站:https://developer.mozilla.org/en-US/docs/Web/HTTP Content-Security-Policy(CSP):禁止 ...

  9. C++的cout高阶格式化操作

    这篇文章主要讲解如何在C++中使用cout进行高级的格式化输出操作,包括数字的各种计数法(精度)输出,左或右对齐,大小写等等.通过本文,您可以完全脱离scanf/printf,仅使用cout来完成一切 ...

  10. e3 cpu

    英特尔® 至强® E3 处理器 https://www.intel.cn/content/www/cn/zh/products/processors/xeon/e3-processors.html?p ...