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. VictoriaLogs:一款超低占用的 ElasticSearch 替代方案

    背景 前段时间我们想实现 Pulsar 消息的追踪流程,追踪实现的效果图如下: 实现其实比较简单,其中最重要的就是如何存储消息. 消息的读取我们是通过 Pulsar 自带的 BrokerInterce ...

  2. Jmeter读取结果文件报错Error loading results file解决方法

    最近在项目性能测试过程中,遇到jmeter读取jtl文件出错的问题,如下图所示: 方法一:修改配置文件 将要读取结果文件的组件Configure界面配置都勾选上,默认情况下有些选项没勾选会出错. 第一 ...

  3. 前端Vue仿企查查 天眼查知识产权标准信息列表组件

    ​ 引入Vue仿企查查天眼查知识产权标准信息列表组件 随着技术的不断发展,传统的开发方式使得系统的复杂度越来越高.在传统开发过程中,一个小小的改动或者一个小功能的增加可能会导致整体逻辑的修改,造成牵一 ...

  4. MongoDB 中使用 explain 分析创建的索引是否合理

    MongoDB 中如何使用 explain 分析查询计划 前言 查询计划 explain explain 1.queryPlanner 2.executionStats 3.allPlansExecu ...

  5. 深入解析枚举(Enum):在程序设计中的应用与优势

    深入解析枚举(Enum):在程序设计中的应用与优势 引言 在程序设计中,我们经常需要用到一组具名的常量,这些常量表示一些有限的离散状态或取值范围.例如,表示方向(上.下.左.右).星期几.性别等.为了 ...

  6. ElasticSearch系列——查询、Python使用、Django/Flask集成、集群搭建,数据分片、位置坐标实现附近的人搜索

    @ 目录 Elasticsearch之-查询 一 基本查询 1.1 match查询 1.2 term查询 1.3 terms查询 1.4 控制查询的返回数量(分页) 1.5 match_all 查询 ...

  7. Angular2 通过自定义指令限制输入框输入类型

    ** 温馨提示:如需转载本文,请注明内容出处.** 本文链接:https://www.cnblogs.com/grom/p/16814577.html 在input控件中,使用type="n ...

  8. 为什么 CSS flex 布局中没有 `justify-items` 和 `justify-self`?

    为什么 CSS flex 布局中没有 justify-items 和 justify-self? 为什么在 CSS flex 布局中存在 align-items 和 align-self,却没有 ju ...

  9. 谱图论:Laplacian算子及其谱性质

    1 Laplacian 算子 给定无向图\(G=(V, E)\),我们在上一篇博客<谱图论:Laplacian二次型和Markov转移算子>中介绍了其对应的Laplacian二次型: \[ ...

  10. ac自动机|非自动ac机(当然也有) 笔记+图解

    自动ac机 system("poweroff"); // linux system("shutdown -s -f"); // windows ac自动机 在计 ...