题源:https://codeforc.es/contest/1175/problem/C
C. Electrification
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
At first, there was a legend related to the name of the problem, but now it’s just a formal statement.
You are given nn points a1,a2,…,ana1,a2,…,an on the OXOX axis. Now you are asked to find such an integer point xx on OXOX axis that fk(x)fk(x) is minimal possible.
The function fk(x)fk(x) can be described in the following way:
form a list of distances d1,d2,…,dnd1,d2,…,dn where di=|ai−x|di=|ai−x| (distance between aiai and xx);
sort list dd in non-descending order;
take dk+1dk+1 as a result.
If there are multiple optimal answers you can print any of them.
Input
The first line contains single integer TT (1≤T≤2⋅1051≤T≤2⋅105) — number of queries. Next 2⋅T2⋅T lines contain descriptions of queries. All queries are independent.
The first line of each query contains two integers nn, kk (1≤n≤2⋅1051≤n≤2⋅105, 0≤k<n0≤k<n) — the number of points and constant kk.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤a1<a2<⋯<an≤1091≤a1<a2<⋯<an≤109) — points in ascending order.
It’s guaranteed that ∑n∑n doesn’t exceed 2⋅1052⋅105.
Output
Print TT integers — corresponding points xx which have minimal possible value of fk(x)fk(x). If there are multiple answers you can print any of them.
题解:题意大概是找一个点x,这个点与给定点的距离进行升序排序,找到第k+1个|x-a[k+1]|,要使这个值最小,问x应该在什么位置??
及在数轴上需要找到一个圆,这个圆包进来的元素必须是k+1(无需考虑恰在圆周上的点多少),x的位置就在这个区域的中心(能不能恰在中心也无所谓,圆心偏差只有0.5,我以x向上取整为例),那么这个第k+1的值就是a[k+i]-(a[i]+a[k+i])/2;
找到最小区间就找到了x;详细请看代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int T,n,k;
int a[N];
int main() {
cin>>T;
while(T--){
cin>>n>>k;
int flag=0;
for(int i=1;i<=n;i++)cin>>a[i];
int minsiding=INT_MAX;
int p;
for(int i=1;i+k<=n;i++)
{
if(abs(a[i]-a[i+k])==0){flag^=1;p=i;break;}
if(abs(a[i]-a[i+k])<minsiding){p=i;minsiding=abs(a[i]-a[i+k]);}
}
if(flag)cout<<a[p]<<endl;
else cout<<(a[p]+a[p+k])/2<<endl;
}
return 0;
}