LIS最长上升子序列



dp[i]保存的是当前到下标为止的最长上升子序列的长度。

模板代码:

int dp[MAX_N], a[MAX_N], n;
int ans = 0; // 保存最大值 for (int i = 1; i <= n; ++i) {
dp[i] = 1;
for (int j = 1; j < i; ++j) {
if (a[j] < a[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
ans = max(ans, dp[i]);
} cout << ans << endl; // ans 就是最终结果

二分优化LIS

复杂度O(nlongn)

int ans[MAX_N], a[MAX_N], dp[MAX_N], n;  // ans 用来保存每个 dp 值对应的最小值,a 是原数组
int len; // LIS 最大值 ans[1] = a[1];
len = 1; for (int i = 2; i <= n; ++i) {
if (a[i] > ans[len]) {
ans[++len] = a[i];
} else {
int pos = lower_bound(ans + 1, ans + len + 1, a[i]) - ans;
ans[pos] = a[i];
}
} cout << len << endl; // len 就是最终结果

动态规划模板1|LIS最长上升子序列的更多相关文章

  1. 动态规划模板2|LCS最长公共子序列

    LCS最长公共子序列 模板代码: #include <iostream> #include <string.h> #include <string> using n ...

  2. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  3. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  4. hdu 5256 序列变换(LIS最长上升子序列)

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...

  5. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

    POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...

  7. 动态规划——E (LIS())最长上升子序列

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  8. 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)

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

  9. LIS最长上升子序列三种方法 (模板)

    O(n^)的方法: #include <iostream> #include <stdio.h> #include <cstring> #include <a ...

随机推荐

  1. 棋盘问题---poj1321(dfs)

    http://poj.org/problem?id=1321 由于搜索是原来写的,而集训的时候没来所以只能现在补补咯-_- 简单的深搜 #include<stdio.h> #include ...

  2. Git 常用命令(转)

    原文:http://www.cnblogs.com/1-2-3/archive/2010/07/18/git-commands.html add by zhj :图是用 思维导图 软件MindMapp ...

  3. visual studio code 的必装推荐插件plugin, vscode, vsc

    An Old Hope Theme     (theme, 推荐,且推荐它的 classic theme,安装后在颜色选项里选择,该插件的定制见文末) Cobalt2     (theme) Drac ...

  4. 详解Linux(centos7)下安装OpenSSL安装图文方法

    OpenSSL是一个开源的ssl技术,由于我需要使用php相关功能,需要获取https的文件所以必须安装这个东西了,下面我整理了两种关于OpenSSL安装配置方法. 安装环境:  操作系统:CentO ...

  5. Spark与Spring集成做web接口

    需要实现的功能: 写访问spark的接口,也就是从web上输入网址就能把我们需要的信息通过提交一个job然后返回给我们json数据. 成果展示: 通过url请求,然后的到一个wordcount的jso ...

  6. [py]可迭代对象-求最值

    for .. in ..方式遍历可迭代对象 而非下标 ## 判断是否可迭代 from collections import Iterable print(isinstance(123,Iterable ...

  7. Variational Approximate Inference

    图模型(Graphical Models)是一个用来表示概率模型的工具.所谓概率模型,也就是在刻画一组随机变量之间的相互关系.图模型就是用来显式地刻画这些变量之间关系的.在 图模型中,每个变量由图中的 ...

  8. [LeetCode] 383. Ransom Note_Easy tag: Hash Table

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  9. Vagrant配置虚拟机

    慕课上学习.需要安装 vagrant  VirtualBox .box文件和.iso文件一样都是镜像文件.可以在官网下载https://www.vagrantup.com/docs/ 点击boxs之后 ...

  10. only_full_group_by问题而引发的对group by的深入思考

    问题背景 最近在项目中使用mysql的group by进行分组查询的场景比较多,其中一次遇到了一个问题,即在开发环境执行一个如下sql时是正确且可执行的, select a,b,max(c) from ...