Longest Ordered Subsequence
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 41944   Accepted: 18453

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

Source

Northeastern Europe 2002, Far-Eastern Subregion
 
方法一:记忆化搜索
缺点:时间复杂度O(n^2)
#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+二分)(优化版)的更多相关文章

  1. Longest Ordered Subsequence POJ - 2533 最长上升子序列dp

    题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  2. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  3. UVa 10534 Wavio Sequence (最长递增子序列 DP 二分)

    Wavio Sequence  Wavio is a sequence of integers. It has some interesting properties. ·  Wavio is of ...

  4. POJ 2533——Longest Ordered Subsequence(DP)

    链接:http://poj.org/problem?id=2533 题解 #include<iostream> using namespace std; ]; //存放数列 ]; //b[ ...

  5. Luogu 3402 最长公共子序列(二分,最长递增子序列)

    Luogu 3402 最长公共子序列(二分,最长递增子序列) Description 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作业: ...

  6. 【bzoj3173】【Tjoi2013】【最长上升子序列】treap+dp二分优化

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61560361 向大(hei)佬(e)实力学(di ...

  7. 【简单dp】poj 1458 最长公共子序列【O(n^2)】【模板】

    最长公共子序列可以用在下面的问题时:给你一个字符串,请问最少还需要添加多少个字符就可以让它编程一个回文串? 解法:ans=strlen(原串)-LCS(原串,反串); Sample Input abc ...

  8. POJ 1159 Palindrome-最长公共子序列问题+滚动数组(dp数组的重复利用)(结合奇偶性)

    Description A palindrome is a symmetrical string, that is, a string read identically from left to ri ...

  9. [poj 1533]最长上升子序列nlogn树状数组

    题目链接:http://poj.org/problem?id=2533 其实这个题的数据范围n^2都可以过,只是为了练习一下nlogn的写法. 最长上升子序列的nlogn写法有两种,一种是变形的dp, ...

随机推荐

  1. nmap 使用脚本引擎进行扫描

    1.下载nmap(nmap官网). 2.安装nmap. 3.编辑环境变量(windows下所需),保存.

  2. NetAnalyzer笔记 之 六 用C#打造自己的网络连接进程查看器(为进程抓包做准备)

    [创建时间:2016-04-13 22:37:00] NetAnalyzer下载地址 起因 最近因为NetAnalyzer2016的发布,好多人都提出是否可以在NetAnalyzer中加入一个基于进程 ...

  3. sql 读取excel中的数据

    select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...

  4. 使用CMD连接SQL Server

      在CMD中操作数据库,界面不美观,而且排版不整齐,但在机器上没有安装SQLSERVER的时候,也是极其方便的.   在命令行中输入 OSQL ?可以获得所有帮助信息   osql -S 数据库服务 ...

  5. ControlStyles(枚举)

    指定控件的样式和行为. 此枚举有一个 FlagsAttribute 特性,通过该特性可使其成员值按位组合.属性: ContainerControl:如果为true,则控件是类似容器的控件. UserP ...

  6. JS时间日期

    JS获取当前时间 var myDate = new Date(); myDate.get[UTC]FullYear();    //获取完整的年份(4位,1970-????)myDate.get[UT ...

  7. tomcat免安装版注册为系统服务

    环境: OS:windows7_64bit JDK:jdk1.6_64bit tomcat:apache-tomcat-7.0.61-windows-x64 1.修改tomcat/bin/servic ...

  8. iOS — Autolayout之Masonry解读

    前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...

  9. web 安全 初探 (正在更新)

    1.web应用程序所采用的防卫机制的几个核心构成:1.处理用户对应用程序的数据和功能的访问,以防止用户未经授权访问.2.处理用户的输入,以防止恶意的输入导致未预期的行为.3.处理攻击,以确保应用程序在 ...

  10. push方法的页面间跳转--

    一,自定义动画写push方法-- 添加coreGraphics.framework框架 在CATransitionAnimation.h文件里面引入-- #import <QuartzCore/ ...