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, ...
随机推荐
- SQL数据库,使用事务执行增删改操作,给自己一个后悔的机会
内容并不复杂,使用起来也比较简单. 主要使用以下3条SQL语句: 开始事物:BEGIN TRAN(全拼 TRANSACTION 亦可)提交事物:COMMIT TRAN回滚事务:ROLLBACK TRA ...
- #1000 A + B (hihoCoder)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 求两个整数A+B的和 输入 输入包含多组数据.每组数据包含两个整数A(1 ≤ A ≤ 100)和B(1 ≤ A ≤ 100) ...
- JavaScript贷款计算器
今天花了两个小时模仿书上代码用JS制作了JavaScript贷款计算器,时间有些长,但相比以前,自己细心了不少,每天进步一点点,量的积累达到质的飞跃 <!doctype html>< ...
- JAVA 中文转GBK内码方法
不能谷歌,百度了很久,没有直接的转换方法,参考 byte[]数组与十六进制字符串与字符串的互相转换 http://blog.163.com/roadwalker@126/blog/static/113 ...
- rpc使用JUnit模块测试设计的方法及常见问题
RPC:Remote Procedure Call 远程过程调用 Wikipedia:http://en.wikipedia.org/wiki/Remote_Procedure_Call 百度百科:h ...
- Mac上安装与更新Ruby,Rails运行环境
Mac安装后就安装Xcode是个好主意,它将帮你安装好Unix环境需要的开发包,也可以独立安装command_line_tools_for_xcode 1.安装RVM RVM:Ruby Version ...
- SharePoint Framework 配置Office 365开发者租户
博客地址:http://blog.csdn.net/FoxDave 你需要一个Office 365开发者租户来使用预览版SharePoint Framework构建和发布客户端web部件.你的租户 ...
- C#编程:SqlCommand.Parameters.Add()方法的参数问题。
在存储过程中添加2个参数 sql语句 例: “update [tablename] username = @username where id=@id” 然后把需要的 command.Paramete ...
- SSH面试题收藏
Hibernate工作原理及为什么要用? 原理: 1. 读取并解析配置文件2. 读取并解析映射信息,创建SessionFactory3. 打开Sesssion4. 创建事务Transation5. 持 ...
- Mac下手动安装SafariDriver extension
环境:Mac OS X Yosemite 10.10.4下, Safari 8 Step 1:第一次运行SafariDriver时,先找到WebDriver extension的安装路径,比如/Use ...