题目描述见链接 .
将 放到数轴中, 题意转化 为: 在数轴上选择一个点 , 使得与 距离第 大的点 到 的距离 最小 .
显然是具有 单调性 的, 考虑 二分答案, 二分出 , 再考虑怎么检查 是否合法,
当 中的 点数 不超过 个时, 说明 合法,
但是直接枚举 去检查 是否合法显然会 , 考虑更快的方法,
枚举 左端点 , 设 , 右端点 ,
若 , 说明 时, 距离 第 近的点距离不超过 , 符合题目要求 .
于是就可以 了 .
#include<bits/stdc++.h>
#define reg register
const int maxn = 2e5 + 10;
int read(){
char c;
int s = 0, flag = 1;
while((c=getchar()) && !isdigit(c))
if(c == '-'){ flag = -1, c = getchar(); break ; }
while(isdigit(c)) s = s*10 + c-'0', c = getchar();
return s * flag;
}
int N;
int K;
int Ans;
int A[maxn];
bool check(int x){
for(reg int i = 1; i+K-1 <= N; i ++){
int j = i + K - 1;
if(A[j]-A[i] <= (x << 1)){ Ans = A[i]+A[j] >> 1; return true; }
}
return false;
}
void Work(){
N = read(), K = read() + 1;
for(reg int i = 1; i <= N; i ++) A[i] = read();
int l = 0, r = A[N] - A[1];
while(l <= r){
int mid = l+r >> 1;
if(check(mid)) r = mid - 1;
else l = mid + 1;
}
printf("%d
", Ans);
}
int main(){
int T = read();
while(T --) Work();
return 0;
}