A11995 | Reach Median
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains two integers $n$ and $s$ ( $1\le n\le 2\cdot 10^5-1$ , $1\le s\le 10^9$ ) — the length of the array and the required value of median.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1\le a_i \le 10^9$ ) — the elements of the array $a$ .
It is guaranteed that $n$ is odd.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1\le a_i \le 10^9$ ) — the elements of the array $a$ .
It is guaranteed that $n$ is odd.
输出格式
In a single line output the minimum number of operations to make the median being equal to $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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted