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, ...
随机推荐
- SQLServer存储过程和触发器学习记录及简单例子
一.存储过程 存储过程即为能完成特定功能的一组SQL语句集.如果需要对查出的多条数据进行操作的话,这里需要理解游标(CURSOR)的概念,对于oracle有for each row命令,可以不用游标 ...
- windows7 64位系统pl/sql 客户端的安装
解压将下载到的将其解压,如我解压到了 E:\app\instantclient_11_2 3.设置PLSQL Developer在tools-prefrences,conncetion,OCI lib ...
- 64位Win7 VS调试、PLSQL与oracle的连接异常问题
系统换为64位Win7后,VS与Oracle开发环境出现了很多问题.调试无法连接Oracle,PLSQL无法连接Oracle等一系列问题.下面记录一下处理办法: 1.oracle客户端选择32位进行安 ...
- 整合了一个功能强大完善的OA系统源码,php全开源 界面漂亮美观
整合了一个功能强大完善的OA系统源码,php全开源界面漂亮美观.需要的同学联系Q:930948049
- sublime text 3 快捷键大全
Sublime Text 3 快捷键精华版 Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所 ...
- java语法基本知识2
对象的引用相当于C中的指针.对象存在于堆中,引用存在于栈中.引用在函数,可以称作局部变量.
- 使用checkbox做出radiobutton的效果
首先分别使用不同name的checkbox放在同一个tr的两个td内.并为他们设置onclick事件来改变点击后状态. php: str[info]="<table align='ce ...
- 解决ie6 fixed 定位以及抖动问题
像你所遇到的问题一样, IE6浏览器有太多的bug让制作网页的人头疼.这篇文章介绍的是介绍的是如何解决IE6不支持position:fixed;属性的办法.如果我们需要做某个元素始终位于浏览器的底部, ...
- adb catlog>d:\log.txt日志级别
W,警告 I,通知 D,调试 E,错误 V, 到最细的日志 功能测试可以看W和E,性能测试I比较有用
- 在linux使用make编译ArduPilot for Pixhawk/PX4 ArduPilot 编译环境搭建
Building ArduPilot for Pixhawk/PX4 on Linux with Make 使用Make编译 ArduPilot for Pixhawk 2, Pixhawk and ...