原题链接: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 小的数分成了若干个区间,假设区间长度为,那么这个区间里可以选的数就是 L−K+1 L − K + 1 个,所以我们把每个区间里最小的 L−k+1 L − k + 1 个数都拿出来放到一起,最后拿出来的数里第 Q Q 小的那个就是删掉的数里面最大的。
那么我们只需要枚举一遍所有最小值,每次取就好了,大功告成,总复杂度 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;
}
本文介绍了一道AtCoder上的RMQ问题,目标是在一系列操作后找到已删除元素中最大值与最小值之差的最小可能值。通过巧妙地将序列分割并选择合适的子序列进行操作来解决此问题。
2942

被折叠的 条评论
为什么被折叠?



