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

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.

翻译

提醒:奇数元素数组[a1,a2,…,a2k+1]的中值定义如下:让[b1,b2,…,b2k+1]按排序顺序作为数组元素。这个数组的中值等于bk+1。

有2n个学生,第1个学生有技能水平的人工智能。并不能保证所有的技能水平都是不同的。

我们把一个班的技能水平定义为该班学生技能水平的中位数。

作为学校的校长,你想把每个学生分配到两个班级中的一个,这样每个班级都有奇数个学生(不能被2整除)。根据你的选择,班上的学生人数可以是相等的,也可以是不同的。每个学生必须被分配到一个班级。在这些分区中,您希望选择一个将类的技能级别之间的绝对差异最小化的分区。

你能达到的最小绝对差是多少?

每个测试包含多个测试用例。第一行包含测试用例的数量t(1≤t≤104)。下面是对测试用例的描述。

每个测试用例的第一行包含一个整数n(1≤n≤105)-学生人数减半。

每个测试用例的第二行包含2n个整数a1,a2,…,a2n(1≤ai≤109)-学生的技能水平。

保证所有测试用例的n之和不超过105。

提示

输出

对于每个测试用例,输出一个整数,即两类奇数大小的技能水平之间的最小可能绝对差异。

输入
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
输出
0
1
5
在第一个测试中,只有一种方法来划分学生-每个班级一个。技能等级的绝对差异将为1-1=0。

在第二个测试中,一个可能的划分是使第一类学生的技能水平为[6,4,2],这样第一类学生的技能水平为4,第二类学生的技能水平为[5,1,3]。绝对差为| 4−3 |=1。

注意,不能像[2,3]、[6,5,4,1]或[]、[6,5,4,1,2,3]这样分配,因为班级的学生数是偶数。

[2] [1,3,4]也不可能,因为技能5和6的学生没有被分配到一个班级。

在第三个测试中,您可以按以下方式分配学生:[3,4,13,13,20],[2,5,8,16,17]或[3,8,17],[2,4,5,13,13,16,20]。两个分区都给出最小的可能绝对差。

思路

将所有的数字分为两组,再两组中分别找到中位数使两组中位数的绝对值的差最小。

可以先排序使a1<a2<a3…<a(2n),将a1,a3,a5…a(2n-1)分一组,将a2,a4,a6…a(2*n)分一组在组中得到的中位数即是要得到中位数。

用数学分析得到an,an+1,分别是第一个,第二个数组的中位数。

为了节省时间用sort函数实现快排

代码实现

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int look;
    cin >> look;

    while(look--)
    {
        int x,i,doit;
        cin >> x;
        doit=2*x;
        int num[1000000];
        memset(num,0,sizeof(num));

        for(i=1; i<=doit; i++)
        {
            cin >> num[i];
        }

        sort(num+1,num+1+doit);

        int s;
        s=num[x+1]-num[x];
        cout << s <<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值