题目传送门


Reminder: the median of the array [a1,a2,…,a2k+1][a1,a2,…,a_{2k+1}][a1,a2,…,a2k+1​] of odd number of elements is defined as follows: let [b1,b2,…,b2k+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 bk+1b_{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;
}

CF 1300.B——Assigning to Classes【思维】相关推荐

  1. CF 1567 C. Carrying Conundrum(思维)

    CF 1567 C. Carrying Conundrum(思维) 题目大意: 题目大意很有意思,就是一个小孩他不会列式相加,把进位加到了该位左侧的第二位上,现在给出结果,问按照这个小孩的方法,能有多 ...

  2. Assigning to Classes CodeForces - 1300B

    Reminder: the median of the array [a1,a2,-,a2k+1][a1,a2,-,a2k+1] of odd number of elements is define ...

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

    Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...

  4. B. Assigning to Classes

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. 贪心 ---- Codeforces Round #618 (Div. 2)B. Assigning to Classes+贪心[证明过程]

    题目链接 题目大意:给你2∗n2*n2∗n个数,将这些数分成2个集合使得两个集合中位数的差值最小 解题思路:我懵了一个结论就是排序之后取中间的两个数然后就ac了 我们先对这些数字从小到大排序 证明:1 ...

  6. 【CF#-931A】 Friends Meeting(思维)

    题干: Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in ...

  7. cf 、B. Fridge Lockers(思维 + 贪心)

    题意:有n个冰箱,然后m个锁链,每条锁链两头都连着冰箱,然后让你每个冰箱都至少连着两条锁链指向不同冰箱.买锁链的钱是两个冰箱重量的和,问最少花费多少钱可以满足,如果满足输出最小花费,否则,输出-1. ...

  8. cf 1153D-Serval and Rooted Tree(思维,树形dp)

    传送门 题意 给定一棵具有 n n n 个结点的数,每个结点都被标有 m a x max max 或 m i n min min 标记,分别表示该点的值将取其所有儿子结点中的最大值或最小值(对于叶子结 ...

  9. NOIP2016 总结

    今年的NOIP停了3个星期的课,本以为状态不错,可以有良好的发挥,但是考试结束后却因为一些平时没注意到的细节导致考挂了. 第一天 第一题还是比较符合NOIP的难度,就是一个模拟题.但第二题就开始画风突 ...

最新文章

  1. java 组合框_Java知多少(88)列表和组合框
  2. 企业管理,难的是什么?
  3. 硬件语言编写规范与技巧
  4. python处理excel表格大小-如何用python处理excel表格
  5. VBA 中判断汉字的方法
  6. python编译后的pyd爆破
  7. 121 Best Time to Buy and Sell Stock
  8. class类初始化之后调用赋值问题记录
  9. 晨哥真有料丨对她越好,越难脱单!
  10. 为什么把钱花在别人身上更幸福?
  11. Flutter进阶—质感设计之标签栏
  12. 图解DotNet框架之一:编译与执行引擎(上)
  13. nginx websocket wss 连接失败 failed_浅谈WebSocket协议、WS协议和WSS协议原理及关系
  14. 【学习笔记】bootstrap之CSS组件
  15. 科技护肤品,买还是不买
  16. html 制作魔方源代码,CSS3 3d环境实现立体 魔方效果代码
  17. coreldraw怎么打印荣誉证书_CorelDraw如何批量打印证书
  18. CIE颜色空间是如何用来设计更好的led的
  19. 拼装机器人感想_拼装机器人 感受未来科技
  20. 阿里云mysql主从有延迟怎么办_MySQL主从数据库同步延迟问题解决-阿里云开发者社区...

热门文章

  1. python--数据挖掘开头(KNN使用,OneR介绍)
  2. 目前互联网的技术发展现状和趋势如何?
  3. 微软 bing 壁纸 每日一图 bing api
  4. mysql报错1517_错误日志 userenv ID1524 1517
  5. 触动千亿电商市场 BitCherry星耀雅加达
  6. python:2019新年贺词的词云制作以及基于TF-IDF的关键词提取
  7. 【微信辅助】疫情当前,python帮你找出朋友圈的武汉朋友给予关怀
  8. Mondo Rescue备份iso镜像文件与安装系统
  9. 豪杰DVD播放器 3.0.8 URL重新定向栈溢出漏洞
  10. OpenStack高级控制服务之使用编配服务(Heat)实现自动化部署云主机