median 中位数 odd 奇数 even 奇数

You are given an array aa of nn integers and an integer ss. It is guaranteed that nn 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 ss.

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,86,5,8 is equal to 66, since if we sort this array we will get 5,6,85,6,8, and 66 is located on the middle position.

Input

The first line contains two integers nn and ss (1≤n≤2⋅105−11≤n≤2⋅105−1, 1≤s≤1091≤s≤109) — the length of the array and the required value of median.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array aa.

It is guaranteed that nn is odd.

Output

In a single line output the minimum number of operations to make the median being equal to ss.

Examples
input

Copy
3 8
6 5 8
output

Copy
2
input

Copy
7 20
21 15 12 11 20 19 12
output

Copy
6
Note

In the first sample, 66 can be increased twice. The array will transform to 8,5,88,5,8, which becomes 5,8,85,8,8 after sorting, hence the median is equal to 88.

In the second sample, 1919 can be increased once and 1515 can be increased five times. The array will become equal to 21,20,12,11,20,20,1221,20,12,11,20,20,12. If we sort this array we get 11,12,12,20,20,20,2111,12,12,20,20,20,21, this way the median is 2020.

题意:给你一个含有n个数字的数组,问改动多少次才能使这个数组的中位数为s,每次改动只能使一个数字加1或减1

分析:先给这个数组从低到高排序,找出其中的中位数,从后面到中位数的位置遍历,即(n to n/2+1),判断,如果其中有数字小于s,则改动次数加上s-a[i],当i等于n/2+1时,判断,如果大于了s,改动次数加上a[i]-s;再从(1 to n/2)遍历,如果有a[i]>s,改动次数加上a[i]-s.

总之,要使中位数之前的都小于等于中位数,中位数之后的都大于等于中位数。

 #include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
long long n,s,a[];
while(~scanf("%lld %lld",&n,&s))
{
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+,a++n);
long long sum=;
for(int i=n;i>=n/+;i--)
{
if(a[i]<s)
sum+=s-a[i];
if(i==n/+&&a[i]>s)
sum+=a[i]-s;
}
for(int i=n/;i>=;i--)
{
if(a[i]>s)
sum+=a[i]-s;
}
printf("%lld\n",sum);
}
return ;
}

1037B--Reach Median(中位数)的更多相关文章

  1. spark rdd median 中位数求解

    lookup(key) Return the list of values in the RDD for key key. This operation is done efficiently if ...

  2. 【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) B】Reach Median

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 将数组排序一下. 考虑中位数a[mid] 如果a[mid]==s直接输出0 如果a[mid]<s,那么我们把a[mid]改成s ...

  3. NOI.AC 20181103 题解

    CF 1037B  Reach Median 班上 n个同学(n 是奇数)排成一排站队,为了美观,需要大家高度的中位数是 x. 你可以让同学们在脚下垫木板或者稍微蹲一点来达成这个目标.对任意一位同学的 ...

  4. Find Median from Data Stream 解答

    Question Median is the middle value in an ordered integer list. If the size of the list is even, the ...

  5. JMeter常见问题集合

    前言 本文内容仅仅是针对Jmeter的部分功能名词的介绍和解释,以及初学者不易理解的问题的整理.部分内容来自别人做的整理,为了更好地整理自己的思路,所以可耻的整理一下发到博客上. 标题[1-6]和[参 ...

  6. JMeter 问题

    1.  JMeter 测试计划 测试计划 使用 JMeter 进行测试的起点,是其它 JMeter 测试元件的容器. 线程组 代表一定数量的并发用户,它可以用来模拟并发用户发送请求.实际的请求内容在S ...

  7. R语言-数据高级管理

    数学函数 abs() 绝对值 sqrt() 平方 ceiling() 向上取整 floor() 向下取整 trunc() 截取整数部分 round(x,digits = n) 保留几位小数 统计函数 ...

  8. 【摘】 pt-query-digest工具一解

    原文 http://blog.csdn.net/seteor/article/details/24017913 1.percona-toolkit安装 wget http://www.percona. ...

  9. Python数据分析之pandas学习

    Python中的pandas模块进行数据分析. 接下来pandas介绍中将学习到如下8块内容:1.数据结构简介:DataFrame和Series2.数据索引index3.利用pandas查询数据4.利 ...

随机推荐

  1. 时间同步chrony

          时间同步chrony [root@compute02 ~]# yum install chrony   编辑配置文件 将sever区块下的内容修改为时间服务器的地址 .此处可以写局域网内的 ...

  2. jvm感知docker容器参数

    docker中的jvm检测到的是宿主机的内存信息,它无法感知容器的资源上限,这样可能会导致意外的情况. -m参数用于限制容器使用内存的大小,超过大小时会被OOMKilled. -Xmx:  默认为物理 ...

  3. html地址--待更新

    11.学习笔记: 视频直播技术:ijkplayer技术:jni技术: https://www.cnblogs.com/renhui/category/1011048.html: IM:环信, xmpp ...

  4. js浮点数运算封装, 起因财务部分精确计算

    目录 背景 具体代码 背景 项目中用到浮点数,Int 等 js中 Number类型比较多, 加上牵涉到财务软件, 前台js运算等. 有时候会出现精确度的问题 , 公共方法中有好事者写的方法. 此处拿来 ...

  5. 第8章 传输层(1)_TCP/UDP协议的应用场景

    1. 传输层的两个协议 1.1 TCP和UDP协议的应用场景 (1)TCP协议:如果要传输的内容比较多,需要将发送的内容分成多个数据包发送.这就要求在传输层用TCP协议,在发送方和接收方建立连接,实现 ...

  6. Linux IO多路复用 select/poll/epoll

    Select -- synchronius I/O multiplexing select, FS_SET,FD_CLR,FD_ISSET,FD_ZERO #include <sys/time. ...

  7. Getting Physical With Memory.CPU如何操作内存

    原文标题:Getting Physical With Memory 原文地址:http://duartes.org/gustavo/blog/ [注:本人水平有限,只好挑一些国外高手的精彩文章翻译一下 ...

  8. 在windows server 2012/2016上,任务管理器性能页面增加磁盘监控的办法

    从windows server 2012开始,微软修改了任务管理器的显示方式,图像化看起来更直观了,但是可惜的是,默认情况下,2012和2016均只显示CPU/内存/网络三个资源监视,没有重要的磁盘, ...

  9. SDOI2018IIIDX

    /* 题目转换为 n个节点的一片森林,n个权值,要给每个节点分配一个权值,保证子节点的权值不小于父节点的权值,并且1~n的权值的字典序最大. 考场上的贪心很显然 建立出 树来 将所有数值从大到小排序 ...

  10. windows 下安装 docker

    1. 使用阿里云的镜像进行安装: http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 2. 安装完成后点击图标 “Dock ...