CF1037B.Reach Median
普及-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a of n integers and an integer s. It is guaranteed that n is odd.
In one operation you can either increase or decrease any single element by one. Calculate the minimum number of operations required to make the median of the array being equal to s.
The median of the array with odd length is the value of the element which is located on the middle position after the array is sorted. For example, the median of the array 6,5,8 is equal to 6, since if we sort this array we will get 5,6,8, and 6 is located on the middle position.
给你一个包含 n 个整数的数组 a 和一个整数 s。保证 n 是奇数。
在一次操作中,你可以将任意一个元素增加或减少 1。请计算使数组的中位数等于 s 所需的最少操作次数。
对于长度为奇数的数组,其中位数定义为:将数组排序后位于中间位置的元素的值。例如,数组 6,5,8 的中位数为 6,因为对该数组排序后得到 5,6,8,而 6 正好位于中间位置。
输入格式
The first line contains two integers n and s (1≤n≤2⋅105−1, 1≤s≤109) — the length of the array and the required value of median.
The second line contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array a.
It is guaranteed that n is odd.
第一行包含两个整数 n 和 s(1≤n≤2⋅105−1,1≤s≤109)—— 分别表示数组的长度和所需的中位数值。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 表示数组 a 的元素。
保证 n 为奇数。
输出格式
In a single line output the minimum number of operations to make the median being equal to s.
在一行中输出使中位数等于 s 所需的最少操作次数。
输入输出样例
输入#1
3 8 6 5 8
输出#1
2
输入#2
7 20 21 15 12 11 20 19 12
输出#2
6
说明/提示
In the first sample, 6 can be increased twice. The array will transform to 8,5,8, which becomes 5,8,8 after sorting, hence the median is equal to 8.
In the second sample, 19 can be increased once and 15 can be increased five times. The array will become equal to 21,20,12,11,20,20,12. If we sort this array we get 11,12,12,20,20,20,21, this way the median is 20.
在第一个样例中,6 可以增加两次。数组将变为 8,5,8,排序后为 5,8,8,因此中位数为 8。
在第二个样例中,19 可增加一次,15 可增加五次。数组将变为 21,20,12,11,20,20,12。对该数组排序后得到 11,12,12,20,20,20,21,此时中位数为 20。
输入解题思路,AI测评打分。不知道怎么写?