动态规划模板1|LIS最长上升子序列
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最长上升子序列的更多相关文章
- 动态规划模板2|LCS最长公共子序列
LCS最长公共子序列 模板代码: #include <iostream> #include <string.h> #include <string> using n ...
- 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
- POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- hdu 5256 序列变换(LIS最长上升子序列)
Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...
- POJ 3903 Stock Exchange (E - LIS 最长上升子序列)
POJ 3903 Stock Exchange (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...
- POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)
POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...
- 动态规划——E (LIS())最长上升子序列
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...
- 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- LIS最长上升子序列三种方法 (模板)
O(n^)的方法: #include <iostream> #include <stdio.h> #include <cstring> #include <a ...
随机推荐
- Django的调试方法
web程序调试起来和桌面程序有着很大的差别,对于Django程序来说调试更是个问题.我们可以用postman发送http请求,下面就介绍几种调试方法: 1.在Eclipse+Pydev中调试Djang ...
- 关于HttpServletRequest报红叉的解决办法
今天导入项目的时候,发现报错了,如题.然后找到了解决办法 解决方法:鼠标右击项目工程——>Build Path——>点击comfigure Build Path进入----->选择j ...
- scrapy的post登录:renren
# -*- coding: utf-8 -*- import scrapy class RenrenSpider(scrapy.Spider): name = 'renren' allowed_dom ...
- js屏蔽f12键
<script> $(document).keydown(function(e) { if (e.keyCode == 123) {/ ...
- HTML输入框的默认显示内容
在某些情况下我们会需要在输入框里默认显示一些内容,比如在登录的时候不在输入框前面显示用户名和密码,直接在输入框里显示,这时只要在input的标签里添加属性 placeholder="用户名 ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- Weka——PrincipalComponents分析
package weka.filters.unsupervised.attribute; PrincipalComponents 属性: /** The data to transform analy ...
- Chapter 7 Integrity(完整性), Views(视图), Security(安全性), and Catalogs(目录)
from Database Design to Physical Form CREATE TABLE integrity constraints (完整性约束) CREATE VIEW Securit ...
- Vue.js学习笔记——表单控件实践
最近项目中使用了vue替代繁琐的jquery处理dom的数据更新,个人非常喜欢,所以就上官网小小地实践了一把. 以下为表单控件的实践,代码敬上,直接新建html文件,粘贴复制即可看到效果~ <! ...
- Linux命令:删除与恢复命令
敲命令按以下顺序 ①vim filename ②e ③i ④ESC 删除命令: x(小写):删除光标所在处字符. dd:删除光标所在的行. D:删除从光标所在之处开始直到该行末尾的全部字符. < ...