Problem Statement

You are given a sequence $P = (p_1,p_2,\ldots,p_N)$ that contains $1,2,\ldots,N$ exactly once each.

You may perform the following operations between $0$ and $K$ times in total in any order:

  • Choose one term of $P$ and remove it.
  • Move the last term of $P$ to the head.

Find the lexicographically smallest $P$ that can be obtained as a result of the operations.

Constraints

  • $1 \leq N \leq 2 \times 10^5$
  • $0 \leq K \leq N-1$
  • $1 \leq p_i \leq N$
  • $(p_1,p_2,\ldots,p_N)$ contains $1,2,\ldots,N$ exactly once each.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $K$
$p_1$ $p_2$ $\ldots$ $p_N$

Output

Print the lexicographically smallest $P$ that can be obtained as a result of the operations, separated by spaces.


Sample Input 1

5 3
4 5 2 3 1

Sample Output 1

1 2 3

The following operations make $P$ equal $(1,2,3)$.

  • Removing the first term makes $P$ equal $(5,2,3,1)$.
  • Moving the last term to the head makes $P$ equal $(1,5,2,3)$.
  • Removing the second term makes $P$ equal $(1,2,3)$.

There is no way to obtain $P$ lexicographically smaller than $(1,2,3)$, so this is the answer.


Sample Input 2

3 0
3 2 1

Sample Output 2

3 2 1

You may be unable to perform operations.


Sample Input 3

15 10
12 10 7 2 8 11 9 1 6 14 3 15 13 5 4

思路不算特别难,但超多细节。建议拿正确代码对拍。

考虑直接贪心。首先第一位要尽量小。我们可以从前 \(k+1\) 个数中选,亦可以从倒数 \(k\) 个中选。如果从倒数 \(k\) 个选,我们可以直接把后面 \(k\) 个中最小的转到前面。如果从前 \(k+1\) 个选,我们就把最小的那位的前面删掉就可以了。

假设我们现在确定了第一位,那么后面我们可以用类似单调栈的方法维护最小字典序字符串。就可以了。单调栈删元素的时候要特判,当某一位是从后面转过来的时候,我们可以把转的那一步改为删除。所以不消耗次数。

不知道赛时怎么写出来这一题。

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int n,k,a[N],st[N],j,q[N],l=2e5,r=2e5-1,t[N],ret,tp,p,g,f[N];
int main()
{
scanf("%d%d",&n,&k),j=k;
for(int i=1;i<=n;i++)
scanf("%d",a+i),f[a[i]]=i;
ret=2e9;
for(int i=1;i<=k+1;i++)
ret=min(ret,a[i]);
// printf("%d ",ret);
if(!k)
{
for(int i=1;i<=n;i++)
printf("%d ",a[i]);
putchar('\n');
return 0;
}
j=k;
for(int i=1;i<=k+1;i++)
{
if(a[i]==ret)
{
for(int k=i;k<=n;k++)
{
while(j&&p&&st[p]>a[k])
--p,--j;
st[++p]=a[k];
}
break;
}
--j;
}
while(j&&p)
--p,--j;
ret=2e9;
for(int i=1;i<=k;i++)
ret=min(ret,a[n-i+1]);
j=k;
for(int i=1;i<=k;i++)
{
q[--l]=a[n-i+1],--j;
if(a[n-i+1]==ret)
{
g=i;
for(int k=1;k<=n-i;k++)
q[++r]=a[k];
break;
}
}
for(int i=l;i<=r;i++)
{
while((j||f[t[tp]]>=(n-g+1))&&tp&&t[tp]>q[i])
j-=f[t[tp]]<(n-g+1),--tp;
t[++tp]=q[i];
}
while((j||f[t[tp]]>=(n-g+1))&&tp)
--tp,--j;
for(int i=1;i<=max(tp,p);i++)
{
if(t[i]>st[i]||i>p)
{
for(int j=1;j<=p;j++)
printf("%d ",st[j]);
return 0;
}
if(t[i]<st[i]||i>tp)
{
for(int j=1;j<=tp;j++)
printf("%d ",t[j]);
return 0;
}
}
for(int i=1;i<=tp;i++)
printf("%d ",t[i]);
return 0;
}

[ABC262F] Erase and Rotate的更多相关文章

  1. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  2. 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)

    一.移除性算法 (remove)  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  3. 剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)

    一.移除性算法 (remove)  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  4. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

  5. leetcode解题报告(20):Rotate Array

    描述 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  6. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

  7. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  8. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. jQuery.rotate.js参数

    CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...

  10. CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)

    CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)   在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...

随机推荐

  1. 安装win10虚拟机

    1.前期工作 下载win10镜像:zh-cn_windows_10_consumer_editions_version_21h1_updated_aug_2021_x64_dvd_4de56d76.i ...

  2. Linux 内核音频子系统调试

    debugfs 文件系统 debugfs 可以为 Linux 内核各个模块的分析调试,提供许多信息,如音频子系统的 ASoC,以及 tracing 等.debugfs 文件系统可以通过命令行工具挂载, ...

  3. 产品代码都给你看了,可别再说不会DDD(四):代码工程结构

    这是一个讲解DDD落地的文章系列,作者是<实现领域驱动设计>的译者滕云.本文章系列以一个真实的并已成功上线的软件项目--码如云(https://www.mryqr.com)为例,系统性地讲 ...

  4. Postgresql 批量插入命令COPY使用

    在很多场景下,我们经常会遇到将某个Excel或Csv文件中的数据,插入到Postgresql.对于这个需求,我们常规的处理办法就是将文件中的数据,按照文件表头名称转换成集合对象然后插入到数据库,当然这 ...

  5. C与CPP常见编译工具链与构建系统简介

    笔者最近在研究CEF的CMake工程,心血来潮想要对各种编译工具链以及构建系统做一个简单的总结,于是就有了本文.本文不会讲解任何关于C/C++语言方面的内容,主要C/C++的编译出发,介绍各种编译工具 ...

  6. 正则表达式快速入门三: python re module + regex 匹配示例

    使用 Python 实现不同的正则匹配(从literal character到 其他常见用例) reference python regular expression tutorial 目录 impo ...

  7. 记一次 .NET 某电力系统 内存暴涨分析

    一:背景 1. 讲故事 前些天有位朋友找到我,说他生产上的程序有内存暴涨情况,让我帮忙看下怎么回事,最简单粗暴的方法就是让朋友在内存暴涨的时候抓一个dump下来,看一看大概就知道咋回事了. 二:Win ...

  8. Web3.0时代的全新合作模式:DAO

    你有没有遇到这种情况:我有一个很棒的想法,想要开发出一个"改变世界"的项目,但是我既没有技术,也没有人脉,甚至没有资金,导致我始终没有办法开始行动,痛苦万分.就比如在黑客大赛上,我 ...

  9. qq群匿名聊怎么用

    qq群匿名聊怎么用 1 2 3 4 5 分步阅读 匿名的意思就是不认识.群匿名聊当然是把群里的马甲一下变成不认识的人,再在一起聊天.是不是觉得有点吃饱了没事干,但是当下该功能还是比较实用的,群匿名聊可 ...

  10. 我试图扯掉这条 SQL 的底裤。

    你好呀,我是歪歪. 这次带大家盘一个我觉得有点意思的东西,也是之前写<一个烂分页,踩了三个坑!>这篇文章时,遇到的一个神奇的现象,但是当时忙着做文章搞定这个主线任务,就没有去深究这个支线任 ...