Longest Increasing Subsequence
很久不写算法了== 写个东西练练手
最长上升子序列
输入n,然后是数组a[ ]的n个元素
输出最长上升子序列的长度
一、最简单的方法复杂度O(n * n)
- DP[ i ] 是以a[ i ] 为结尾的最长上升子序列的长度。
- DP[ i ] = max{DP[ j ] + 1 | j < i && a[ j ] < a[ i ]}
代码:
/*
* =====================================================================================
* Filename : LongestIncrSub1.cpp
* Description : O(n^2)
* Version : a better Algorithm of O(n^2)
* Created : 03/22/14 22:03
* Author : Liu Xue Yang (LXY), liuxueyang457@163.com
* Motto : How about today?
* =====================================================================================
*/
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstdlib>
;
int dp[MAXN], a[MAXN];
int n, i, j;
int
main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("LongestIncrSub.txt", "r", stdin);
#endif /* ----- not ONLINE_JUDGE ----- */
while ( ~scanf("%d", &n) ) {
; i < n; ++i ) {
scanf ( "%d", &a[i] );
dp[i] = INT_MAX;
}
; i < n; ++i ) {
; j < n; ++j ) {
|| dp[j-] < a[i] ) {
if ( dp[j] > a[i] ) {
dp[j] = a[i];
}
}
}
}
;
; j >= ; --j ) {
if ( dp[j] != INT_MAX ) {
result = j + ;
break;
}
}
printf ( "%d\n", result );
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
二、因为长度相同的几个不同的子序列中,最末位数字最小的在之后比较有优势,所以用DP针对这个最小的末尾元素求解。
DP[ i ] 表示长度为 i + 1的上升子序列中末尾元素的最小值
从前往后扫描数组a[ ],对于每一个元素a[ i ],只需要在DP[ ] 数组中找到应该插入的位置。
if j == 0 || a[ i ] > DP[ j-1 ]
DP[ j ] = min{ DP[ j ], a[ i ]}
由于对于每个a[ i ] 都要扫描一遍DP[ ] 数组,所以复杂度还是O(n * n)
代码:
/*
* =====================================================================================
* Filename : LongestIncrSub1.cpp
* Description : O(n^2)
* Version : a better Algorithm of O(n^2)
* Created : 03/22/14 22:03
* Author : Liu Xue Yang (LXY), liuxueyang457@163.com
* Motto : How about today?
* =====================================================================================
*/
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstdlib>
;
int dp[MAXN], a[MAXN];
int n, i, j;
int
main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("LongestIncrSub.txt", "r", stdin);
#endif /* ----- not ONLINE_JUDGE ----- */
while ( ~scanf("%d", &n) ) {
; i < n; ++i ) {
scanf ( "%d", &a[i] );
dp[i] = INT_MAX;
}
; i < n; ++i ) {
; j < n; ++j ) {
|| dp[j-] < a[i] ) {
if ( dp[j] > a[i] ) {
dp[j] = a[i];
}
}
}
}
;
; j >= ; --j ) {
if ( dp[j] != INT_MAX ) {
result = j + ;
break;
}
}
printf ( "%d\n", result );
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
三、对于上一个算法,在DP[ ]数组中找a[ i ]元素的插入位置的时候,采用的是线性查找,由于DP[ ]这个数组是有序的,所以可以采用二分,这要复杂度就降到了O(nlogn),可以用STL函数lower_bound用来找第一个大于等于a[ i ]的位置。
代码:
/*
* =====================================================================================
* Filename : LongestIncrSub2.cpp
* Description : A better solution
* Version : algorithm of O(nlogn)
* Created : 03/22/14 22:37
* Author : Liu Xue Yang (LXY), liuxueyang457@163.com
* Motto : How about today?
* =====================================================================================
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <algorithm>
using namespace std;
;
int a[MAXN], dp[MAXN];
int i, n, result;
int
main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
freopen("LongestIncrSub.txt", "r", stdin);
#endif /* ----- not ONLINE_JUDGE ----- */
while ( ~scanf("%d", &n) ) {
fill(dp, dp + n, INT_MAX);
; i < n; ++i ) {
scanf ( "%d", &a[i] );
}
; i < n; ++i ) {
*lower_bound(dp, dp + n, a[i]) = a[i];
}
result = lower_bound(dp, dp + n, INT_MAX) - dp;
printf ( "%d\n", result );
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
Source Code on GitHub
四、如何打印出最长上升子序列呢?
用一个position数组,position[ i ] 表示位置 i 的数字在上升子序列中的位置。也就是,插入dp数组中的位置。
比如


然后在position数组中从后往前找到第一次出现的3对应的a[ i ] = 8,然后接着找第一次出现的2对应的a[ i ] = 3,然后接着找第一次出现的1对应的a[ i ] = 2,最后接着
找第一次出现的0对应的a[ i ] = -7
所以,-7, 2, 3, 8就是最长上升子序列的一个解。这个解是在序列中最后出现的。
代码:
/*
* =====================================================================================
* Filename : LongestIncrSub2.cpp
* Description : A better solution
* Version : algorithm of O(nlogn)
* Created : 03/22/14 22:37
* Author : Liu Xue Yang (LXY), liuxueyang457@163.com
* Motto : How about today?
* =====================================================================================
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <algorithm>
using namespace std;
;
int a[MAXN], dp[MAXN], position[MAXN], sub[MAXN];
int i, n, result;
int
main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
// freopen("LongestIncrSub.txt", "r", stdin);
#endif /* ----- not ONLINE_JUDGE ----- */
while ( ~scanf("%d", &n) ) {
fill(dp, dp + n, INT_MAX);
; i < n; ++i ) {
scanf ( "%d", &a[i] );
}
int *tmp;
; i < n; ++i ) {
tmp = lower_bound(dp, dp + n, a[i]);
position[i] = tmp - dp;
*tmp = a[i];
}
result = lower_bound(dp, dp + n, INT_MAX) - dp;
printf ( "%d\n", result );
;
; i >= ; --i ) {
if ( t == position[i] ) {
sub[t] = a[i];
--t;
}
}
; i < result; ++i ) {
if ( i ) {
printf ( " " );
}
printf ( "%d", sub[i] );
}
printf ( "\n" );
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
所有的代码在git里面
Longest Increasing Subsequence的更多相关文章
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [tem]Longest Increasing Subsequence(LIS)
Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...
- [LintCode] Longest Increasing Subsequence 最长递增子序列
Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...
- Leetcode 300 Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- [LeetCode] Longest Increasing Subsequence
Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- 300. Longest Increasing Subsequence
题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)
https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- IOS UIButton用法详解
这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用. //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...
- easyui tabs update后tab上关闭图标失效的解决方案
问题:使用easyui的tabs组件的时候,调用了tab的update方法,更新后的tab标签上的关闭图标失效 使用的js文件是1.3版本的jquery.easyui.min.js文件,通过读源文件发 ...
- 【转】RadControls for Silverlight(学习1-GridView)
引用:Telerik(官 网:http://www.telerik.com/)是保加利亚的一个软件公司,专注于微软.Net平台的表示层与内容管理控件.我们提供高度稳定性和丰富性能的组件产品,并可应用在 ...
- install graph-tool
try this if ubuntu version is >= 14.04 sudo apt-get update sudo apt-get upgrade sudo apt-get -y i ...
- 19:A*B问题
总时间限制: 1000ms 内存限制: 65536kB 描述 输入两个正整数A和B,求A*B. 输入 一行,包含两个正整数A和B,中间用单个空格隔开.1 <= A,B <= 50000 ...
- Node相关参考资料
参考资料: [玩转Nodejs日志管理log4js]http://blog.fens.me/nodejs-log4js/ [dependencies与devDependencies之间的区别]http ...
- C++笔试题
平时学术必须用Python多啊,但校招笔试绕不开语言基础,非cs科班小弱临阵整理些C++题备考.很弱很苦逼... 一.指针 1.二维数组指针 #include<stdio.h> int m ...
- VS2010的项目配置
一直对VS的项目配置都是不怎么了解的,以前用过点,半年不用后,什么都忘记了... 下面这个是免于输入过长的引用头文件的,比如:#include “D:/C++/curl-7.37.0/libcurl/ ...
- SpringMvc静态资源加载出错
使用mvc:resource配置 web.xml配置是rest风格的/ 服务器启动没问题 访问地址是报404 另外用了default-servlet的方法加载,服务器启动没错,jsp页面加载静态资源要 ...
- 我们正在等待一次技术革命的到来; We are waiting for the arrival of a technological revolution
In the future, there must be a significant technological revolution just like Industrial Revolution. ...