POJ-2533最长上升子序列(DP+二分)(优化版)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 41944 | Accepted: 18453 |
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
Source
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <stack>
- #include <queue>
- #include <list>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- const int INF=0x3f3f3f3f;
- const double eps=1e-;
- const double PI=acos(-1.0);
- #define maxn 1100
- int a[maxn];
- int dp[maxn];
- int dfs(int p)
- {
- if(dp[p] != -) return dp[p];
- int res = ;
- for(int i = ; i < p; i++)
- if(a[p] > a[i])
- res = max(res, dfs(i)+);
- dp[p] = res;
- return res;
- }
- int main()
- {
- int n;
- while(~scanf("%d", &n))
- {
- memset(dp, -, sizeof dp);
- for(int i = ; i < n; i++)
- scanf("%d", &a[i]);
- int pp = -;
- for(int j = ; j < n; j++)
- {
- pp = max(pp, dfs(j)+);
- }
- //printf("%d\n", dfs(n-1)+ 1);
- printf("%d\n", pp);
- }
- return ;
- }
方法二:dp+二分
其中low_bound 返回第一个大于它的数的下标。
缺点:无法保存每个以 a[i]结尾的最长上升子序列。
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <stack>
- #include <queue>
- #include <list>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- const int INF=0x3f3f3f3f;
- const double eps=1e-;
- const double PI=acos(-1.0);
- #define maxn 11000
- int a[maxn];
- int dp[maxn];
- int main()
- {
- int n;
- while(~scanf("%d", &n))
- {
- for(int i = ; i < n; i++)
- scanf("%d", &a[i]);
- int cnt = ;
- //memset(dp, INF, sizeof dp);
- dp[cnt] = a[];
- for(int i = ; i < n; i++)
- {
- if(a[i] > dp[cnt])
- {
- dp[++cnt] = a[i];
- }
- else
- {
- int pos = lower_bound(dp,dp+cnt+,a[i]) - dp;
- dp[pos] = a[i];
- }
- }
- printf("%d\n", cnt+);
- }
- return ;
- }
方法三:dp+二分(优化版)
弥补了上面两种方法不足。时间复杂度为O(nlogn) 又能保存每个以a[i]结尾的最长上升子序列。
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <stack>
- #include <queue>
- #include <list>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- const int INF=0x3f3f3f3f;
- const double eps=1e-;
- const double PI=acos(-1.0);
- #define maxn 11000
- int a[maxn];
- int b[maxn];
- int dp[maxn];
- int main()
- {
- int n;
- while(~scanf("%d", &n))
- {
- for(int i = ; i < n; i++)
- scanf("%d", &a[i]);
- memset(dp, , sizeof dp);
- memset(b, INF, sizeof b);
- for(int i = ; i < n; i++)
- {
- int pos = lower_bound(b,b+n,a[i]) - b;
- dp[i] = pos+;
- b[pos] = a[i];
- }
- int ans = -;
- for(int i = ; i < n; i++)
- ans = max(ans, dp[i]);
- printf("%d\n", ans);
- }
- return ;
- }
POJ-2533最长上升子序列(DP+二分)(优化版)的更多相关文章
- Longest Ordered Subsequence POJ - 2533 最长上升子序列dp
题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- POJ 1458 最长公共子序列(dp)
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...
- UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)
Wavio Sequence Wavio is a sequence of integers. It has some interesting properties. · Wavio is of ...
- POJ 2533——Longest Ordered Subsequence(DP)
链接:http://poj.org/problem?id=2533 题解 #include<iostream> using namespace std; ]; //存放数列 ]; //b[ ...
- Luogu 3402 最长公共子序列(二分,最长递增子序列)
Luogu 3402 最长公共子序列(二分,最长递增子序列) Description 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作业: ...
- 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...
- 【简单dp】poj 1458 最长公共子序列【O(n^2)】【模板】
最长公共子序列可以用在下面的问题时:给你一个字符串,请问最少还需要添加多少个字符就可以让它编程一个回文串? 解法:ans=strlen(原串)-LCS(原串,反串); Sample Input abc ...
- POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)
Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...
- [poj 1533]最长上升子序列nlogn树状数组
题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...
随机推荐
- Java Web----Java Web的数据库操作(一)
Java Web的数据库操作 一.JDBC技术 1.JDBC简介 JDBC是Java程序与数据库系统通信的标准API,它定义在JDK的API中,通过JDBC技术,Java程序可以非常方便地与各种数据库 ...
- HTML浅识
HTML相关======= ## 认识网页 *web标准(w3c三种标准):结构标准 -->html 表现标准 -->css 行为标准 -->js **浏览器和服务器:浏览器发送报文 ...
- WebGIS在行业中应用的演变
结合我本身的项目及WebGIS在公检法行业中的应用,对此作了一个演变过程的总结: 第一阶段:GIS基本功能的应用:Data Show(数据展示):Search(搜索):Search b ...
- Ruby中,类方法和实例方法的一个有趣的例子
最初的代码如下: class Object def abc p "instance abc" end def self.abc p "class abc" en ...
- 老漏洞easy击:CVE-2012 0158占顶!
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXF1c2hp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Dynamics CRM 2016 使用Plug-in Trace Log调试插件
1.写插件 首先,让我们写一个简单的插件来测试新插件跟踪日志功能.请注意,在下面的示例代码中,我们增加ITracingService的一个实例,以及记录有关插件的执行信息记录的一些键值: 2.注册插件 ...
- [转]iOS开发使用半透明模糊效果方法整理
转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...
- <经验杂谈>C#/.Net字符串操作方法小结
字符串操作是C#中最基本的.最常见的.也是用的最多的,以下我总结 了几种常见的方法 1.把字符串按照分隔符转换成 List /// <summary> /// 把字符串按照分隔符转换成 L ...
- DEV GridControl 导出到Excel
SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel"; sa ...
- JS属性读写操作+if判断注意事项
js中不允许出现“ - ” 页面中改变文字大小-案例: <!doctype html> <html lang="en"> <head> < ...