CF 1300.B——Assigning to Classes【思维】

这道题目要求将2n个学生的技能水平分成两个奇数大小的班级,使得班级间的技能水平中位数之差的绝对值最小。通过排序找到最中间的两个数作为各班级的中位数,可以达到最小的差异。给出多个测试用例,每个用例包含学生人数和他们的技能水平,输出每个测试用例的最小绝对差。
摘要由CSDN通过智能技术生成

题目传送门


Reminder: the median of the array [ a 1 , a 2 , … , a 2 k + 1 ] [a1,a2,…,a_{2k+1}] [a1,a2,,a2k+1] of odd number of elements is defined as follows: let [ b 1 , b 2 , … , b 2 k + 1 ] [b1,b2,…,b2_{k+1}] [b1,b2,,b2k+1] be the elements of the array in the sorted order. Then median of this array is equal to b k + 1 b_{k+1} 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

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

For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.


input

3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16


output

0
1
5


Note

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.


题意

  • 给2n个数,分两组,求中位数差值的绝对值最小

题解

  • 排序后,中间相邻的两个分别作为两个班级的中位数必定最优。

  • 因为两个中位数必定一个在前半段,一个在后半段,那么显然中间两个差值最小。


AC-Code

#include<bits/stdc++.h>
using namespace std;
 
const int maxn = 1e5 + 7;
int a[maxn << 1];
int main() {
	int T;	cin >> T; while (T--) {
		int n; cin >> n;
		for (int i = 1; i <= 2 * n; ++i)
			cin >> a[i];
		sort(a + 1, a + 1 + n * 2);
		cout << a[n + 1] - a[n] << endl;
	}
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值