Educational Codeforces Round 66 (Rated for Div. 2), problem: (C) Electrification

题源: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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Macarons_i

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

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

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

打赏作者

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

抵扣说明:

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

余额充值