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: 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
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.
Example
inputCopy
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
outputCopy
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.
#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;
}
}
Codeforces Round #618 (Div. 2)-B. Assigning to Classes的更多相关文章
- Codeforces Round #618 (Div. 2)
题库链接 https://codeforces.ml/contest/1300 A. Non-zero 一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数 显然如果有0的话,必须 ...
- Codeforces Round #618 (Div. 2) 小号上紫之路
这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
- Codeforces Round #618 (Div. 1)C(贪心)
把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 #define HAVE_STRU ...
- Codeforces Round #618 (Div. 1)B(几何,观察规律)
观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...
- Codeforces Round #618 (Div. 1)A(观察规律)
实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...
- Codeforces Round #618 (Div. 2)A. Non-zero
Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...
- Codeforces Round #618 (Div. 2)C. Anu Has a Function
Anu has created her own function ff : f(x,y)=(x|y)−y where || denotes the bitwise OR operation. For ...
- Codeforces Round #618 (Div. 2)-Non-zero
Guy-Manuel and Thomas have an array a of n integers [a1,a2,-,an]. In one step they can add 1 to any ...
随机推荐
- How to setup a Alpine Linux mirror
How to setup a Alpine Linux mirror Contents 1 Introduction 2 Setting up the cron job 3 Setting up ...
- lr集合点
集合点:rendezvous point =====>>loadrunner的虚拟用户中,并发concurrent\ 同时simultaneous的区别: concurrent并发:指虚拟 ...
- 03-css3中的3D转换
一.CSS3-3D转换 1.3D 特点:近大远小,物体和面遮挡不可见 1.1三维坐标系 x 轴:水平向右 -- x 轴右边是正值,左边是负值 y 轴:垂直向下 -- y 轴下面是正值,上面是负值 z ...
- 数据类型、运算符、Scanner的使用
一.常见的基本数据类型 数值型 byte(最小,2字节) short(4字节) int (默认 8字节) long(16字节) 浮点型 f ...
- php+mysql数据库联合查询 left join 右侧数据重复问题
情况:多表联合查询(三表及以上联合查询) 分析: A left join B left join C left join D 假如: 表B.C.D都与表A关联查询 A left join B 4条数据 ...
- 2016NOIP普及组T2回文日期
回文日期 分类:枚举,函数 [题目描述] 日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后2位代表日期 ...
- 基于netty实现rpc框架-spring boot客户端
上篇讲了RPC服务端的实现.原理就是解析netty通道数据拿到类.方法及入参等信息,然后通过java反射机制调用本地接口返回结果.没有用到很复杂的技术. 这篇我们将客户端的实现.说白了客户端的任务很简 ...
- Java SE —— 专栏总集篇
前言: Java 语言,是相对于其他语言而言,门槛低,而且功能还强大的一门编程语言,本人十分看好这一门语言,但是,它也是有深度的,看过本人的<数据结构与算法>专栏的同学们有福了,因为本人在 ...
- [数据库]Mysql蠕虫复制增加数据
将查询出来的数据插入到指定表中,例: 将查询user表数据添加到user表中,数据会成倍增加 insert into user(uname,pwd) select uname,pwd from use ...
- C#集合类型——Hashtable、Dictionary之浅谈
Hashtable表 数组.数组集合.List集合都是通过索引来访问里面成员.哈希表则是通过键来访问成员值.键不可为空,值可为空. 比如: Hashtable hash=new Hashtable ...