前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #618 (Div. 2)-B. Assigning to Classes

Codeforces Round #618 (Div. 2)-B. Assigning to Classes

作者头像
风骨散人Chiam
发布2020-10-29 12:39:00
5580
发布2020-10-29 12:39:00
举报
文章被收录于专栏:CSDN旧文
代码语言:javascript
复制
Reminder: the median of the array [a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1] be the elements of the array in the sorted order. Then median of this array is equal to bk+1.

There are 2n students, the i-th student has skill level ai. It's not guaranteed that all skill levels are distinct.

Let's define skill level of a class as the median of skill levels of students of the class.

As a principal of the school, you would like to assign each student to one of the 2 classes such that each class has odd number of students (not divisible by 2). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.

What is the minimum possible absolute difference you can achieve?

Input

代码语言:javascript
复制
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤105) — the number of students halved.

The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤109) — skill levels of students.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output

代码语言:javascript
复制
For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.

Example inputCopy

代码语言:javascript
复制
3
1
1 1
3
6 5 4 1 2 3
5

13 4 20 13 2 5 8 3 17 16 outputCopy

代码语言:javascript
复制
0
1
5

Note

代码语言:javascript
复制
In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be |1−1|=0.

In the second test, one of the possible partitions is to make the first class of students with skill levels [6,4,2], so that the skill level of the first class will be 4, and second with [5,1,3], so that the skill level of the second class will be 3. Absolute difference will be |4−3|=1.

Note that you can't assign like [2,3], [6,5,4,1] or [], [6,5,4,1,2,3] because classes have even number of students.
 

[2], [1,3,4] is also not possible because students with skills 5 and 6 aren't assigned to a class.

In the third test you can assign the students in the following way: [3,4,13,13,20],[2,5,8,16,17] or [3,8,17],[2,4,5,13,13,16,20]. Both divisions give minimal possible absolute difference.
代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
const int N=5e5;
#define read(a) scanf("%d",&a);
long long a[N];
int main()
{
	int t;
	read(t);
	while(t--){
        int n;
        read(n);
        for(int i=1;i<=2*n;i++)
            cin>>a[i];
        sort(a+1,a+2*n+1);
        cout<<a[n+1]-a[n]<<endl;
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/02/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档