POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]
题目链接:http://poj.org/problem?id=2533
Time Limit: 2000MS Memory Limit: 65536K
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
题意:
给定长度为 $N$ 的一串整数序列 $a[1 \sim N]$,求其最长上升子序列的长度。
注意:子序列可以不连续,要求严格单增。
题解:
$O(n \log n)$ 解法——贪心+二分。
构建一个栈 $S$ 和一个变量 $top$ 代表栈顶位置,该栈的代表:栈中的第 $i$ 个数 $S[i]$,是序列 $a$ 中,长度为 $i$ 的递增子序列的末尾元素。
初始化 S[top=]=a[] ,即将第一个数字入栈;这很好理解,到目前为止 $a[1]$ 自己是一个长度为 $1$ 的递增子序列。
遍历 $a[ i = 2 \sim N ]$:每次对于 $a[i]$,找出栈 $S[1 \sim top]$ 中第一个大于等于 $a[i]$ 的数的位置 $pos$,若不存在则返回 $pos=top+1$。
这是由于,若存在第一个大于等于 $a[i]$ 的数 $S[pos]$ ,说明对于长度为 $pos$ 的递增子序列,可以用 $a[i]$ 代替掉其原来的末尾元素 $S[pos]$,这样一来,依然是一个长度为 $pos$ 的递增子序列,而且该递增子序列被进一步“加长”的潜力增加。而如果栈中不存在大于等于 $a[i]$ 的数,这说明我可以在目前长度为 $top$ 的递增子序列后面加上一个 $a[i]$,那么我们就得到了一个以 $a[i]$ 为结尾的,长度为 $top+1$ 的递增子序列。
因此,我们把 $S[pos]$ 更新为 $a[i]$,并且尝试更新栈的大小 if(pos>top) top=pos; 。
由于栈 $S$ 中元素始终保持单调递增(而且栈内元素互不相等),所以找 $S$ 中第一个大于等于 $a[i]$ 的数可以使用二分查找。
AC代码(在OpenJudge百练提交):
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+; int n;
vector<int> a; int S[maxn],top;
int LIS(const vector<int>& a)
{
S[top=]=a[];
for(int i=;i<a.size();i++)
{
int pos=lower_bound(S,S+top+,a[i])-S;
S[pos]=a[i], top=max(top,pos);
}
return top+;
} int main()
{
cin>>n;
while(n--)
{
int x; cin>>x;
a.push_back(x);
}
cout<<LIS(a)<<endl;
}
PS.我们可以看到,求第一个大于等于 $a[i]$ 的数使用了lower_bound,相应的如果我们使用upper_bound会怎么样呢?不难证明,我们将会得到最长不下降子序列的长度。
POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]的更多相关文章
- 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)
两种算法 1. O(n^2) #include<iostream> #include<cstdio> #include<cstring> using namesp ...
- POJ 2533 Longest Ordered Subsequence 最长递增序列
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 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)
一.Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequenc ...
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
- 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...
- POJ 2533 Longest Ordered Subsequence(最长上升子序列(NlogN)
传送门 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subseque ...
随机推荐
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- auto类型说明符
auto让编译器通过出初始值来推算变量的类型,显然,auto定义的变量必须有初始值: //由val1和val2相加的结果可以推断出item的类型 auto item = val1 + val2;//i ...
- Python-web应用 +HTTP协议 +web框架
web架构 # web应用 架构# C/S 架构 | B/S 架构# client server: 客户端服务器架构,C++# browser server:浏览器服务器架构,Java.Python ...
- django日志,django-crontab,django邮件模块
django 日志 四大块,格式器,过滤器,处理器,日志管理器 LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatt ...
- Atcoder Grand Contest 032
打的第一场Atcoder,已经知道会被虐得很惨,但没有想到竟然只做出一题-- 思维急需提升. A - Limited Insertion 这题还是很签到的. 感觉正着做不好做,我们反着来,把加数变为删 ...
- 洛谷P5206 [WC2019]数树 [容斥,DP,生成函数,NTT]
传送门 Orz神仙题,让我长了许多见识. 长式子警告 思路 y=1 由于y=1时会导致后面一些式子未定义,先抓出来. printf("%lld",opt==0?1:(opt==1? ...
- php中echo、print、print_r、var_dump、var_export区别
(1) print和echo是语言结构,echo输出多个值,多个值之间用逗号分隔,无返回值:print只能输出一个值,有返回值.echo和print输出整型和字符串,没法打印布尔型,如果true,打印 ...
- Web Application Vulnerablities
1. File inclusion berfoe start this caption i make a conclusion for install third-part as follow I ...
- query string parameters 、 Form Data 、 Request Payload
微信小程序ajax向后台传递参数的时候总是报400错误 然后看了一下network 发现是query string parameters,但是我写的header如下 header:{ "Co ...
- INotifyPropertyChanged 接口
INotifyPropertyChanged 接口 用于向客户端(通常是执行绑定的客户端)发出某一属性值已更改的通知. 例如,考虑一个带有名为 FirstName 属性的 Person 对象.若要提供 ...