题意

  给你一个长度为 \(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 普及题的更多相关文章

  1. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  2. 【题解】AcWing 110. 防晒(普及题)

    [题解]AcWing 110. 防晒(普及题) AcWing 110. 防晒 你没有用过的全新OJ 嘿嘿水水题. 题目就是一维坐标轴上给定多个线段,给定多个点,点在线段上造成贡献,点可以重复,问最大贡 ...

  3. 解题报告:hdu1257 LIS裸题

    2017-09-02 17:28:44 writer:pprp 那个裸题练练手,讲解在之前的博客中提到了 代码如下: /* @theme:hdu1257 @writer:pprp @begin:17: ...

  4. POJ-2533.Longest Ordered Subsequence (LIS模版题)

    本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...

  5. 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 ...

  6. UVA10534:Wavio Sequence(最长递增和递减序列 n*logn)(LIS)好题

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68553#problem/B 题目要求: Wavio是一个整数序列,具有以下特性 ...

  7. POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)

    题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...

  8. HDU 1257——最少拦截系统——————【LIS变型题】

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. HDU 1069 Monkey and Banana DP LIS变形题

    http://acm.hdu.edu.cn/showproblem.php?pid=1069 意思就是给定n种箱子,每种箱子都有无限个,每种箱子都是有三个参数(x, y, z)来确定. 你可以选任意两 ...

随机推荐

  1. 作业类型维护流程(CO)

    一.建立作业类型——kl01 目的: 藉由做作業類型執行生產報工 目錄路徑: 會計à成本控制à成本中心會計à主檔資料à作業類型à個別處理à KL01 - 建立 Transaction   Code: ...

  2. Apache POI读取Excel

    1.pom.xml配置文件 <!-- 配置Apache POI --> <dependency> <groupId>org.apache.poi</group ...

  3. Pytorch-创建tensor

    引言 本篇介绍创建tensor的几种方式 Import from numpy from_numpy() float64 是 double 类型,也就是说从numpy导入的float其实是double类 ...

  4. ExtJs6.2环境配置

    http://extjs.org.cn/node/831 注意环境变量就是把  创建的项目文件放在C盘中

  5. elasticsearch-head-master下运行npm install报npm WARN elasticsearch-head@0.0.0 license should be a valid SPDX license expression

    2个月没有启动es和es配套服务,今天运行时,发现如下问题: 运行npm install 出现npm WARN elasticsearch-head@0.0.0 license should be a ...

  6. 【ABAP系列】【第五篇】SAP ABAP7.50 之用户接口

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列][第五篇]SAP ABAP7.5 ...

  7. TensorFlow实战第三课(可视化、加速神经网络训练)

    matplotlib可视化 构件图形 用散点图描述真实数据之间的关系(plt.ion()用于连续显示) # plot the real data fig = plt.figure() ax = fig ...

  8. AI 资源帖

    https://github.com/mnielsen/neural-networks-and-deep-learning openCV笔记 https://blog.csdn.net/qq_2689 ...

  9. Quartz持久化到mongodb

    springboot中集成quzrtz ,持久到mongodb 1.pom引用 <?xml version="1.0" encoding="UTF-8"? ...

  10. [爬虫] BeautifulSoup库

    Beautiful Soup库基础知识 Beautiful Soup库是解析xml和html的功能库.html.xml大都是一对一对的标签构成,所以Beautiful Soup库是解析.遍历.维护“标 ...