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, ...
随机推荐
- 简单拨号器(Android)
感受: 1.了解了intent中的action和Uri. 2.了解了向下一个活动传递数据. 3.了解了内容提供器. 4.了解自定义适配器. 4.其实T9拨号器和简单计算器原理一样.
- WPF - 如何引用external dll中图片
前几天一直想引用别的DLL里面的一个图片.总是出现各种各样问题,一气之下将图片拷贝到当前Project中,运行良好.虽然知道引用图片,就1.2.列出来的2点就够了. 1. The Build Acti ...
- Javascript:作用域 学习总结
作用域(scope): 变量与函数的可访问范围,控制着变量与函数的可见性和生命周期 作用域分类: javascript中,变量的作用域分为:全局作用域,局部作用域 局部变量的优先级大于全局变量,或 ...
- 深入浅出理解iOS经常使用的正則表達式—基础篇[Foundation]
參考资料:cocoachina的zys475481075的文章 几个单词 Regular ['regjʊlə]adj. 定期的:有规律的 Expression[ɪk'spreʃ(ə)n; ek-] n ...
- java后台訪问url连接——HttpClients
java后台訪问url,并传递数据--通过httpclient方式 须要的包,包可能多几个额外的,假设无用或者冲突删除就可以.httpclient是使用的是4.4.1的版本号:http://downl ...
- BGP
http://network.51cto.com/art/200912/172439.htm http://blog.sina.com.cn/s/blog_b457dde80101cyqr.html ...
- openssl 非对称加密算法DSA命令详解
1.DSA算法概述 DSA算法是美国的国家标准数字签名算法,它只能用户数字签名,而不能用户数据加密和密钥交换. DSA与RSA的生成方式不同,RSA是使用openssl提供的指令一次性的生成密钥(包括 ...
- sqlserver中的rowversion
rowversion 公开数据库中自动生成的唯一二进制数字的数据类型.rowversion通常用作给表行加版本戳的机制,存储大小为8字节.rowversion数据类型只是递增的数字,不保留日期或时间. ...
- VS2013以管理员身份使用
Win8系统: 1.将C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe改为以管理员身份运行. 2.将 ...
- 控制用户的访问之权限、角色【weber出品必属精品】
权限的作用 限制用户对数据的访问 权限的分类 1. 系统权限:能够存取数据库的权限 2. 对象权限:操作数据库对象的内容 系统权限 1.1 如何创建用户: SQL> create user t ...