LIS系列总结
此篇博客总结常见的LIS模型变形的解法。
-------------------------------------------------------------------
〇、LIS的$O(NlogN)$复杂度的dp和$O(N^2)$复杂度的dp就不写了。
一、最长不下降子序列
把$O(NlogN)$复杂度的dp中,查找$\ge a[i]$的函数lower_bound改为查找$> a[i]$的函数upper_bound即可。
#include<bits/stdc++.h>
using namespace std;
;
int n, a[MAXN], dp[MAXN];
int main(){
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
while(~scanf("%d", &n)){
;i < n;++i)scanf("%d", a + i);
memset(dp, 0x3f, sizeof(dp));
;i < n;++i){
int p = upper_bound(dp, dp + n, a[i]) - dp;
dp[p] = a[i];
}
cout << (lower_bound(dp, dp + n, 0x3f3f3f3f) - dp) << endl;
}
;
}
二、LIS输出方案
1、输出任意方案(以下方法其实是输出下标字典序最大的一个LIS)
#include<bits/stdc++.h>
using namespace std;
;
int n, a[MAXN], dp[MAXN], par[MAXN];
map<int, int> mp;
void print(int x){
if(~par[x])print(par[x]);
printf("%d ", a[x]);
}
int main(){
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
while(~scanf("%d", &n)){
;i < n;++i)scanf("%d", a + i);
memset(dp, 0x3f, sizeof(dp));
memset(par, -, sizeof(par));
mp.clear();
;i < n;++i){
int p = lower_bound(dp, dp + n, a[i]) - dp;
dp[p] = a[i];
mp[a[i]] = i;
)par[i] = mp[dp[p - ]];
}
int len = lower_bound(dp, dp + n, 0x3f3f3f3f) - dp;
printf("%d\n", len);
print(mp[dp[len - ]]);
printf("\n");
}
;
}
2、输出LIS下标字典序最小的方案
#include<bits/stdc++.h>
using namespace std;
;
int n, a[MAXN], dp[MAXN], par[MAXN];
map<int, int> mp;
vector<int> v[MAXN];
void print(int x, int y) {
) {
; i < v[x - ].size(); ++i) {
][i]] < a[v[x][y]]){
print(x - , i);
break;
}
}
}
printf("%d ", a[v[x][y]]);
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
while(~scanf("%d", &n)) {
; i < n; ++i)scanf("%d", a + i);
; i < n; ++i)v[i].clear();
memset(dp, 0x3f, sizeof(dp));
memset(par, -, sizeof(par));
mp.clear();
; i < n; ++i) {
int p = lower_bound(dp, dp + n, a[i]) - dp;
dp[p] = a[i];
mp[a[i]] = i;
)par[i] = mp[dp[p - ]];
v[p].push_back(i);
}
int len = lower_bound(dp, dp + n, 0x3f3f3f3f) - dp;
printf("%d\n", len);
print(len - , );
printf("\n\n");
}
;
}
3、输出数值字典序最小的方案
朴素的想法:在第2步的基础上,把所有方案找出来,然后比较数值字典序。
4、输出公差为$d$的LIS(此LIS不一定是最长上升子序列,但肯定是最长的上升的公差为$d$的等差数列)(任意一个即可)
例题:http://codeforces.com/contest/977/problem/F
/********************template head********************/
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define mk(x, y) make_pair(x, y)
#define pln() putchar('\n')
#define cln() (cout << '\n')
#define fst first
#define snd second
#define MOD 1000000007LL
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
;
/********************template head********************/
int n, d, a[MAXN], dp[MAXN], par[MAXN], len;
map<int, int> mp;
void dfs(int x) {
if(~par[x])dfs(par[x]);
printf("%d ", a[x]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output2.txt", "w", stdout);
#endif // ONLINE_JUDGE
while(~scanf("%d%d", &n, &d)) {
; i < n; ++i)scanf("%d", a + i);
memset(par, -, sizeof(par));
mp.clear();
; i < n; ++i) {
map<int, int>::iterator it = mp.find(a[i] - d);
;
else {
dp[i] = dp[it->snd] + ;
par[i] = it->snd;
}
mp[a[i]] = i;
}
int p = max_element(dp, dp + n) - dp;
printf("%d\n", dp[p]);
dfs(p);
printf("\n");
}
;
}
LIS系列总结的更多相关文章
- 动态规划精讲(一)A单串
单串 单串 dp[i] 线性动态规划最简单的一类问题,输入是一个串,状态一般定义为 dp[i] := 考虑[0..i]上,原问题的解,其中 i 位置的处理,根据不同的问题,主要有两种方式: 第一种是 ...
- 小明系列问题――小明序列(LIS)
小明系列问题――小明序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))
链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...
- hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- 小明系列问题——小明序列(Lis 相距大于d的单调上升子序列)
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- hdu_4521_小明系列问题——小明序列(LIS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4521 题意:中文题,不解释 题解:这题就是LIS的加强版,可以用二分的nlogn来做,也可以用线段树的 ...
- querySelectorAll 方法相比 getElementsBy 系列方法区别
最近有人问到querySelectorAll 方法相比 getElementsBy 系列方法区别,一时没想起来说些什么,今天查下文档,总结一下它们的区别,以便自己理解. 1. W3C 标准queryS ...
- hdu----(4521)小明系列问题——小明序列
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- 8.3 LIS LCS LCIS(完结了==!)
感觉这个专题真不好捉,伤心了,慢慢啃吧,孩纸 地址http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28195#overview 密码 ac ...
随机推荐
- UVa 10917 林中漫步
https://vjudge.net/problem/UVA-10917 题意: 给出一个图,求出从1走到2共有多少种走法.前提是他只沿着满足如下条件的道路(A,B)走:存在一条从B出发回家的路径,比 ...
- ActiveMQ---知识点整理
本文来自于csdn,文章通过介绍ActiveMQ的安装,使用,搭建等等,简单整理了ActiveMQ. 本文转自:http://www.uml.org.cn/zjjs/201802111.asp 一.背 ...
- 2018-2019-2 20165332《网络攻防技术》Exp5 MSF基础应用
2018-2019-2 20165332<网络攻防技术>Exp5 MSF基础应用 1.基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit:就是 ...
- Intel IDEA 2018破解(亲测成功)
破解网址:https://jingyan.baidu.com/article/cb5d6105d9b1b1005d2fe074.html
- VS中的生成事件
1:为什么需要使用生成事件? 在实际开发过程中,一个公共使用的类库,在项目生成DLL后需要被复制到不同的目录下被引用,是不是觉得每次生成之后都需要人工复制是很麻烦的一件事情 我们可以利用VS中的项目生 ...
- Linux jdk环境配置模板
export JAVA_HOME=/opt/JAVA/jdk1.8.0_191export JRE_HOME=${JAVA_HOME}/jreexport CLASSPATH=.:${JAVA_HOM ...
- java垃圾回收期如何工作(编程思想)
垃圾回收器如何工作: 在以前的程序语言中,在堆上分配对象的代价十分昂贵,因此读者会自然觉得对Java中所有对象(基本类型除外)都在堆上分配的方式也非常高昂.然而,垃圾回收期对提高对象的创建速度,却具有 ...
- self-taught learning setting && semi-supervised learning
参考文献: 摘于上文献: The more general and powerful setting is the self-taught learning setting, which does n ...
- Python 使用Microsoft SQL Server数据库
软件环境: Windows 7 32bit Python 3.6 Download https://www.python.org/downloads/ 默认安装,并添加环境变量,一路Next ... ...
- Packer 基本试用
安装 使用mac 系统 https://www.packer.io/downloads.html 配置环境变量 可选 sudo nano ~/.bash_profile export PATH=$PA ...