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. [超详细]SpringBoot整合WebSocket

    1. 什么是WebSocket? WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,它允许在浏览器和服务器之间进行实时的.双向的通信.相对于传统的基于请求和响应的 HTTP 协议, ...

  2. 淘宝商品详情 API的使用说明

    淘宝平台提供了 API 接口可以用于获取淘宝商品详情信息.通过 API 接口,我们可以获取到商品的基本信息.价格.评论及评价等详细信息.以下是使用说明: 获取淘宝API账号 在获取淘宝商品详情 API ...

  3. jdk17下netty导致堆内存疯涨原因排查

    背景: 介绍 天网风控灵玑系统是基于内存计算实现的高吞吐低延迟在线计算服务,提供滑动或滚动窗口内的count.distinctCout.max.min.avg.sum.std及区间分布类的在线统计计算 ...

  4. ZooKeeper核心概念和架构

    ZooKeeper简介 ZooKeeper是一个分布式应用程序协调服务,主要用于解决分布式集群中应用系统的一致性问题.它能提供类似文件系统的目录节点树方式的数据存储,主要用途是维护和监控所存数据的状态 ...

  5. Intervals 题解

    Intervals 题目大意 给定 \(m\) 条形如 \((l_i,r_i,a_i)\) 的规则,你需要求出一个长为 \(n\) 的分数最大的 01 串的分数,其中一个 01 串 \(A\) 的分数 ...

  6. Android下音视频对讲演示程序(声学回音消除、噪音抑制、语音活动检测、自动增益控制、自适应抖动缓冲)(2023年07月13日更新)

    Android下音视频对讲演示程序 必读说明 简介   本软件根据<道德经>为核心思想而设计,实现了两个设备之间进行音视频对讲,一般可用于楼宇对讲.智能门铃对讲.企业员工对讲.智能对讲机. ...

  7. LLM探索:为ChatGLM2的gRPC后端增加连续对话功能

    前言 之前我做 AIHub 的时候通过 gRPC 的方式接入了 ChatGLM 等开源大模型,对于大模型这块我搞了个 StarAI 框架,相当于简化版的 langchain ,可以比较方便的把各种大模 ...

  8. 我与Vue.js 2.x 的七年之痒

    --过去日子的回顾(这是个副标题) --其实这是篇广告软文(这是个副副标题) 以下是一些牢骚和感悟,不感兴趣的可以滑倒最下面,嘻嘻. 每每回忆起从前,就感觉时间飞逝,真切的感受到了那种课本中描述的白驹 ...

  9. 【Spring】AOP实现原理

    注册AOP代理创建器 在平时开发过程中,如果想开启AOP,一般会使用@EnableAspectJAutoProxy注解,这样在启动时,它会向Spring容器注册一个代理创建器用于创建代理对象,AOP使 ...

  10. 自然语言处理历史史诗:NLP的范式演变与Python全实现

    本文全面回顾了自然语言处理(NLP)从20世纪50年代至今的历史发展.从初创期的符号学派和随机学派,到理性主义时代的逻辑和规则范式,再到经验主义和深度学习时代的数据驱动方法,以及最近的大模型时代,NL ...