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. [scrapy]实例:爬取jobbole页面

    工程概览: 创建工程 scrapy startproject ArticleSpider 创建spider cd /ArticleSpider/spiders/ 新建jobbole.py # -*- ...

  2. webpack-Hot Module Replacement(热更新)

    模块热替换(Hot Module Replacement) 模块热替换(HMR - Hot Module Replacement)功能会在应用程序运行过程中替换.添加或删除模块,而无需重新加载整个页面 ...

  3. 在Oracle数据库中使用NFS,怎样调优?

    MOS上有好多文章,基本上都跑不了以下三点: Setup can make a big difference 1. Network topology and load 2. NFS mount opt ...

  4. 3 Angular 2 快速上手启动项目Demo

    Angular2.x与Angular1.x 的区别类似 Java 和 JavaScript 或者说是雷锋与雷峰塔的区别,想要运行Angular2需要安装一些第三方依赖,不会像Angular1.x那样, ...

  5. 百度Fex webuploader.js上传大文件失败

    项目上用百度webuploader.js上传文件,option选项里面已经设置单个文件大小,但是上传低于此阈值的文件时仍然不成功. 我现在的理解是,框架是将文件post到后台服务器端的.. 百度发现是 ...

  6. IIS application pool access desktop denied

    https://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permiss ...

  7. mysql查询表的字符集

    mysql查询表的字符集 SHOW CREATE TABLE user;

  8. Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)——无非是在传统遍历过程中修改叶子结点加入后继结点信息(传统是stack记录),然后再删除恢复

    先看看线索二叉树 n个结点的二叉链表中含有n+1(2n-(n-1)=n+1)个空指针域.利用二叉链表中的空指针域,存放指向结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索 ...

  9. 实现静默安装APK的方法

    需要满足的条件: 内置到ROM.即APK包的安装位置是/system/app下. 下面以 test.apk 为例,演示这个操作.需要准备一台已经获得 Root 权限的手机. 1.通过 USB 连接手机 ...

  10. UITableview控件简单介绍

    注意点:数据源方法只能在控制器里设置 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView ...