[ABC262F] Erase and Rotate
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的更多相关文章
- [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 ...
- 从零开始学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 ...
- 剩下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 ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- 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 ...
- Canvas绘图之平移translate、旋转rotate、缩放scale
画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- jQuery.rotate.js参数
CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...
- CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)
CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate) 在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...
随机推荐
- 《SQL与数据库基础》04. SQL-DQL
目录 DQL 基础查询 条件查询 分组聚合 聚合函数 分组查询 结果排序 分页限制 总结 本文以 MySQL 为例 DQL 语法结构: SELECT 字段列表 FROM 表名列表 WHERE 条件列表 ...
- SqlServer表添加字段
IF NOT EXISTS (SELECT * FROM syscolumns WHERE id=object_id('表名') AND name='字段名') ALTER TABLE 表名 ADD ...
- 如何正确实现一个自定义Exception(二)
上一篇<如何正确实现一个自定义 Exception>发布后获得不少 star.有同学表示很担忧,原来自己这么多年一直写错了.其实大家不用过分纠结,如果写的是 .NET CORE 1.0+ ...
- 基于 ActionFilters 的限流库DotNetRateLimiter使用
前言 在构建API项目时,有时出于安全考虑,防止访问用户恶意攻击,希望限制此用户ip地址的请求次数,减轻拒绝服务攻击可能性,也称作限流.接下来,我们就来学习开源库DotNetRateLimiter 如 ...
- android模拟器推荐
最近装了个海马模拟器用来调试cocos2dx-lua游戏. 安装完之后发现, 我之前装的virtual box被替换掉了, 因为海马模拟器要安装它自己匹配版本的virtual box, 所以我之前的装 ...
- 「codeforces - 1633F」Perfect Matching
link. 首先所有的 activated nodes 组合成了一棵以 \(1\) 为根的有根树.询问即求由 activated nodes 组成的树的最大匹配.对于树上最大匹配有一个贪心策略:自底向 ...
- mysql8安装踩坑记
背景:已安装mysql5.7版本 问题一:默认的3306端口被占用 进入mysql5.7的my.ini文件,更改port为3307或者其他未被占用的端口 问题二:Install/Remove of t ...
- 基于TRE文章的非线性模型化线性方法
之前写过一篇有关TRE优化模型详解的博文: https://www.cnblogs.com/zoubilin/p/17270435.html 这篇文章里面的附录给出了非线性模型化线性的方式,具体内容如 ...
- 期中考试成绩出来了。分数在70~80是等级B。
#多输入 空格分割a = input()b = a.split()c = list(map(int, b))print(c) 等级B 描述 期中考试成绩出来了.分数在70-80是等级B. 输入 请 ...
- 解决因对EFCore执行SQL方法不熟练而引起的问题
前言 本文测试环境:VS2022+.Net7+MySQL 因为我想要实现使用EFCore去执行sql文件,所以就用到了方法ExecuteSqlAsync,然后就产生了下面的问题,首先因为方法接收的参数 ...