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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章