很久不写算法了== 写个东西练练手

最长上升子序列

输入n,然后是数组a[ ]的n个元素

输出最长上升子序列的长度

一、最简单的方法复杂度O(n * n)

  1. DP[ i ] 是以a[ i ] 为结尾的最长上升子序列的长度。
  2. 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的更多相关文章

  1. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  2. [tem]Longest Increasing Subsequence(LIS)

    Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&& ...

  3. [LintCode] Longest Increasing Subsequence 最长递增子序列

    Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return ...

  4. Leetcode 300 Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  5. [LeetCode] Longest Increasing Subsequence

    Longest Increasing Subsequence Given an unsorted array of integers, find the length of longest incre ...

  6. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  7. 300. Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For exam ...

  8. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  9. leetcode@ [300] Longest Increasing Subsequence (记忆化搜索)

    https://leetcode.com/problems/longest-increasing-subsequence/ Given an unsorted array of integers, f ...

  10. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. 静态绑定网关,防止ARP攻击

    Windows XP 下 写个批处理文件:内容如下,名称自取 @echo offard -darp -s 192.168.1.1 00-14-78-ef-10-45//绑定IP地址 WIN 7 下 1 ...

  2. solaris tar 命令exclude使用

    压缩时需要排除指定目录,不知道什么原因在solaris中 tar cvf a.tar a --exclude=a/b/log --exclude = a/c/mm7log不生效, 最后使用了如下方法 ...

  3. jacon

    com的线程回收不由java垃圾回收器进行处理,因此,每new一次jacob提供的类就要分配一定大小的内存给该操作,new出来的这个com对象在使用结束之后产生的垃圾java是无法回收的,new出来的 ...

  4. Perl技巧

    项目里面一直用的是Perl,Perl里有各种小技巧就分享在这吧. push(@a, $b) 把b元素压入a数组中, 还可以有 push(@a, [@b]); 那a就成了二维数组了 scalar(@a) ...

  5. ios之JavaScript

    初次接触java脚本,感觉java脚本so interesting!为什么呢?写javascript代码感觉就像是在记流水账,无拐弯抹角,一个字,就是"干",想怎么干就怎么干,哈哈 ...

  6. loadrunner四大部分

    loadrunner主要分一下四部分 1.VuGen  主要进行录制,回放,参数化,脚本修改,可以对脚本进行recording options,General options,runtime opti ...

  7. Spring中的SPEL

    src\dayday\Person.java package dayday;/** * Created by I am master on 2016/11/28. */public class Per ...

  8. 题目1203:IP地址

    题目: http://ac.jobdu.com/problem.php?pid=1203 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3052 解决:1504 题目描述: 输入一个ip地 ...

  9. 【Windows】为节省系统资源,停掉不必要的服务

    1.windows服务名称(注册表名称)和显示名称对照表如下: < 显示名称 状态 服务名称 Application Management demand AppMgmt ASP.NET Stat ...

  10. 【转】Duff's Device

    在看strcpy.memcpy等的实现发现用了内存对齐,每一个word拷贝一次的办法大大提高了实现效率,参加该blog(http://totoxian.iteye.com/blog/1220273). ...