LIS 普及题
题意
给你一个长度为 \(n\) 的序列 \(a\)。
问是否存在一个长度为 \(L\) 的上升子序列,即存在 \(\{x_1,x_2,...,x_L\}(x_1\lt x_2\lt ...\lt x_L)\),使得 \(a_{x_1}\lt a_{x_2}\lt ...\lt a_{x_L}\)。
输出 \(\{a_{x_1}, a_{x_2}, ..., a_{x_L}\}\),若存在多组解,输出 \(\{a_{x_i}\}\) 字典序最小的一组。
题解
设 \(f[i]\) 表示以 \(i\) 为起点的最长上升子序列的长度是多少。这个显然可以倒推序列 \(a\),用朴素的求 LIS 的方法求出。
然后把序列 \(a\) 以 \(a_i\) 从小到大为第一关键字,下标 \(i\) 从小到大为第二关键字排序。显然从前往后贪心选取即可。
具体地,我们依次确定答案序列的每一位。设答案序列还有 \(L\) 位未确定,当扫到 \(a\) 序列的第 \(i\) 位时,若同时满足以下 \(3\) 个条件 $$\begin{cases} a_i\gt 答案序列上一个确定的数 \ i\gt 答案序列上一个确定的位 \ f[i]\ge L \end{cases}$$
则将这个 \(a_i\) 确定为答案序列的下一位显然是最优的。
这样就能保证答案序列越靠前的位越尽量小,字典序也就最小了。
复杂度 \(O(n\log n)\)。
#include<bits/stdc++.h>
#define N 100010
using namespace std;
inline int read(){
int x=0; bool f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
if(f) return x; return 0-x;
}
int n,L,dp[N],g[N],mx;
struct data{int v,pos;}a[N];
inline bool cmp(data a, data b){return a.v==b.v ? a.pos<b.pos : a.v<b.v;}
int binary(int x){
int l=1, r=mx, mid, ans=0;
while(l<=r){
int mid=l+r>>1;
if(g[mid]>x) ans=mid, l=mid+1;
else r=mid-1;
}
return ans;
}
int main(){
n=read(), L=read();
for(int i=1; i<=n; ++i) a[i].v=read(), a[i].pos=i;
for(int i=n; i>0; --i){
dp[i]=binary(a[i].v)+1;
mx=max(mx,dp[i]);
g[dp[i]]=max(g[dp[i]],a[i].v);
}
if(mx<L){printf("impossible\n"); return 0;}
sort(a+1,a+n+1,cmp);
int lstpos=0,lstv=0;
for(int i=1; i<=n; ++i)
if(dp[a[i].pos]>=L && a[i].pos>lstpos && a[i].v>lstv){
printf("%d ",a[i].v);
if(--L==0) return 0;
lstpos=a[i].pos;
lstv=a[i].v;
}
return 0;
}
LIS 普及题的更多相关文章
- POJ 2533 Longest Ordered Subsequence(LIS模版题)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 47465 Acc ...
- 【题解】AcWing 110. 防晒(普及题)
[题解]AcWing 110. 防晒(普及题) AcWing 110. 防晒 你没有用过的全新OJ 嘿嘿水水题. 题目就是一维坐标轴上给定多个线段,给定多个点,点在线段上造成贡献,点可以重复,问最大贡 ...
- 解题报告:hdu1257 LIS裸题
2017-09-02 17:28:44 writer:pprp 那个裸题练练手,讲解在之前的博客中提到了 代码如下: /* @theme:hdu1257 @writer:pprp @begin:17: ...
- POJ-2533.Longest Ordered Subsequence (LIS模版题)
本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...
- poj 3903 poj 2533 (LIS模板题)
pi1 < pi2 < ... < pik, with i1 < i2 < ... < ik. Sample Input 6 5 2 1 4 5 3 3 1 1 1 ...
- UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68553#problem/B 题目要求: Wavio是一个整数序列,具有以下特性 ...
- POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)
题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...
- HDU 1257——最少拦截系统——————【LIS变型题】
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- HDU 1069 Monkey and Banana DP LIS变形题
http://acm.hdu.edu.cn/showproblem.php?pid=1069 意思就是给定n种箱子,每种箱子都有无限个,每种箱子都是有三个参数(x, y, z)来确定. 你可以选任意两 ...
随机推荐
- SSO单点登录统一身份认证系统
什么是单点登录 简单点说就是公司有A,B两个系统,我登录了A系统之后再跳转到B系统可以直接访问,而不需要再次登录B系统. 几种常见的单点登录实现方式 在讲解单点登录之前先讲解几个基本的概念: Cook ...
- Unity中的动画系统和Timeline(1) 普通动画创建
学习使用版本:Unity2017.1.1 目标:给一个Cube创建动画 一:第一种创建方法 1 Windows —— Animation,打开Aniamtion动画界面,此时显示 因为此时没有任何动画 ...
- 5分钟了解图数据库Neo4j的使用
1.图数据库安装与配置 1.1安装与配置 配置path = %NEO4J_HOME%\bin 启动命令:neo4j console web访问:http://localhost:7474 1. ...
- Pandas时间序列和分组聚合
#时间序列import pandas as pd import numpy as np # 生成一段时间范围 ''' 该函数主要用于生成一个固定频率的时间索引,在调用构造方法时,必须指定start.e ...
- [c++] 计算太阳高度角
/* 输入参数: Longitude - 经度(单位"度") Latitude - 纬度(单位"度") Year - 年 Month - 月 Day - 日 H ...
- 在Docker中部署ASP.NET Core 2.2
⒈新建一个ASP.NET Core2.2 Web程序 因为Windows的Docker和Linux的Docker有所不同,本次测试采用的是Linux的Docker,因此没有勾选启用Docker支持. ...
- 【pytorch】学习笔记(二)- Variable
[pytorch]学习笔记(二)- Variable 学习链接自莫烦python 什么是Variable Variable就好像一个篮子,里面装着鸡蛋(Torch 的 Tensor),里面的鸡蛋数不断 ...
- C++练习 | 单向链表类模板(包含类模板中静态变量初始化格式)
#include <iostream> #include <string> using namespace std; template <class T> clas ...
- python多进程,并获取每个进程的返回值
pool = multiprocessing.Pool(processes=10) row = [...] for row in rows: task_id = row[1] img_id = row ...
- DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)
DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★★(5星) 这个时间系列想 ...