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, ...
随机推荐
- 配置Java开发IDE
http://www.cnblogs.com/feichexia/archive/2012/11/07/Vim_JavaIDE.html
- Erlang&RabbitMQ服务安装配置
RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.RabbitMQ据说具有良 ...
- Themida和Winlicense加壳软件脱壳教程
(一)Themida和不用license的Winlicense加壳软件就不说了,直接上脚本脱壳. (二)先看看不同版本OEP的一些小特征: Temida2.1.X.X版本之后的OEP特征(2.0.8. ...
- range()和xrange()
range(): range([start,] stop[, step]) 如: range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range()默认起始点为0 且ra ...
- hibernate的一种报错
Exception in thread "main" java.lang.NoClassDefFoundError: javax/tools/StandardJavaFileMan ...
- SQLserver CASE WHEN
declare @shuzu int set @shuzu=1 select (case @shuzu when '1' then '444' when '2'then '555' end) as ' ...
- Three.js入门
一.前段时候花了些功夫研究了下WebGL,了解了基本实体的实现原理和实现方法,现在回忆就只记得如果要我画个圆形,怀疑都要了我的命(那得画多少个三角形...).功夫不负有心人,今天学习Three.js得 ...
- cocos2d-x 3.0 事件分发机制
在cocos2d-x 3.0中一共有五个事件监听器: 触摸事件(EventListenerTouch) 键盘响应事件 (EventListenerKeyboard) 加速器记录事件(EventList ...
- ArcMap计算PolyLine中点VBA
Dim pGeo As IGeometrySet pGeo = [Shape]Dim pPolyline As IPolylineSet pPolyline = pGeoDim pCurve As I ...
- android activity改变另一个activity ui
android开发之在activity中控制另一个activity的UI更新 转自:http://www.cnblogs.com/ycxyyzw/p/3875544.html 第一种方法: 遇到一 ...