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, ...
随机推荐
- 详解HashMap的内部工作原理
本文将用一个简单的例子来解释下HashMap内部的工作原理.首先我们从一个例子开始,而不仅仅是从理论上,这样,有助于更好地理解,然后,我们来看下get和put到底是怎样工作的. 我们来看个非常简单的例 ...
- javascript笔记7之对象数组
/* var box = new Array(); //声明一个数组,空数组 alert(typeof box); //数组属于object类型 var box = new Array('李炎恢', ...
- testng xml 示例
TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.php 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...
- js基础例子购物车升级版(未优化版)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Android Dependencies小差号引起的问题
问题是由于Android Dependencies小差号引起的,下午一搞Android的哥们在群里说最近导入的几个工程每个都是Android Dependencies报错,小差号,我先说解决方法: 方 ...
- AVL旋转树
执行插入操作可能出现不平衡的情况,当平衡二叉树.AVL这树是一种自平衡二叉树,使二叉树又一次保持平衡.而且查找.插入和删除操作在平均和最坏情况下时间复杂度都是O(log n) AVL树的旋转一共同拥有 ...
- WebSphere配置数据库连接池
通过WebSphere配置数据库连接池一共需要三项: 1.配置连接驱动,在这里叫:JDBC提供程序; 2.配置数据库连接池,在这里叫:配置数据源; 3.配置数据库登录帐号,密码,在这里 ...
- OC-字典&数组运用实例:通讯录的实现
需求实现: 一.定义联系⼈类ContactPerson 实例变量:姓名.性别.电话号码.住址.分组名称. 方法:初始化⽅方法(姓名.电话号码).显⽰示联系⼈信息 二.定义AddressBook类, 封 ...
- 解决android自带textview右侧文字不能对齐的问题
package com.sixin.view; import android.content.Context; import android.graphics.Canvas; import andro ...
- dispatch_async 与 dispatch_get_global_queue 的使用方法
GCD (Grand Central Dispatch) 是Apple公司开发的一种技术,它旨在优化多核环境中的并发操作并取代传统多线程的编程模式. 在Mac OS X 10.6和IOS 4.0之后开 ...