ARC098E Range Minimum Queries

原题链接:https://arc098.contest.atcoder.jp/tasks/arc098_c

Range Minimum Queries

Problem Statement

You are given an integer sequence A of length N and an integer K. You will perform the following operation on this sequence Q times:

Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).
Let X and Y be the values of the largest and smallest element removed in the Q operations. You would like X−Y to be as small as possible. Find the smallest possible value of X−Y when the Q operations are performed optimally.

Constraints

1≤N≤2000
1≤K≤N
1≤Q≤N−K+1
1≤Ai≤109
All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A1 A2 … AN

Output

Print the smallest possible value of X−Y.

Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

In the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1. Thus, the first operation removes A3=1 and now we have A=(4,3,5,2). In the second operation, it is optimal to choose (A2,A3,A4)=(3,5,2) as the contiguous subsequence of length 3 and remove A4=2. In this case, the largest element removed is 2, and the smallest is 1, so their difference is 2−1=1.

Sample Input 2

10 1 6
1 1 2 3 5 8 13 21 34 55

Sample Output 2

7

Sample Input 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

Sample Output 3

451211184

题目大意

给定一个n个数的数列和两个整数数K,Q,执行Q次操作:选择一段长度为K的区间,删除其中的最小值。

问:执行Q次操作后,被删除的数的最小值和最大值之差 的最小值是多少?

N<=2e3, 数据保证合法

题解

假设我们已经知道了删掉的数中的最小值,既然已经有了最小值 Y Y ,那么包含比Y小的数的区间肯定是不能选的了。这样,整个数列就被比 Y Y 小的数分成了若干个区间,假设区间长度为L,那么这个区间里可以选的数就是 LK+1 L − K + 1 个,所以我们把每个区间里最小的 Lk+1 L − k + 1 个数都拿出来放到一起,最后拿出来的数里第 Q Q 小的那个就是删掉的数里面最大的。

那么我们只需要枚举一遍所有最小值,每次取min就好了,大功告成,总复杂度 O(n2log2n) O ( n 2 l o g 2 n ) ,实测不卡常。

代码
#include<bits/stdc++.h>
using namespace std;
const int M=2e3+5;
int que[M],order[M],tmp[M],now[M],tot,top,n,k,q;
void in()
{
    scanf("%d%d%d",&n,&k,&q);
    for(int i=1;i<=n;++i)scanf("%d",&que[i]),order[i]=que[i];
}
void add(int le,int ri)
{
    tot=0;
    if(le>ri||ri-le+1<k)return;
    for(int i=le;i<=ri;++i)tmp[++tot]=que[i];
    sort(tmp+1,tmp+1+tot);
    for(int i=ri-le+2-k;i>=1;--i)now[++top]=tmp[i];
}
int test(int mn)
{
    int p=1;top=0;
    for(int i=1;i<=n;++i)if(que[i]<mn)add(p,i-1),p=i+1;
    if(top<q)return INT_MAX; 
    sort(now+1,now+1+top);
    return now[q]-mn;
}
void ac()
{
    int ans=INT_MAX;
    sort(order+1,order+1+n);que[++n]=-INT_MAX;
    for(int i=1;i<n;++i)ans=min(test(order[i]),ans);
    printf("%d",ans);
}
int main()
{
    in();ac();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值