1037B--Reach Median(中位数)
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.
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.
In a single line output the minimum number of operations to make the median being equal to ss.
3 8
6 5 8
2
7 20
21 15 12 11 20 19 12
6
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(中位数)的更多相关文章
- spark rdd median 中位数求解
lookup(key) Return the list of values in the RDD for key key. This operation is done efficiently if ...
- 【Manthan, Codefest 18 (rated, Div. 1 + Div. 2) B】Reach Median
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 将数组排序一下. 考虑中位数a[mid] 如果a[mid]==s直接输出0 如果a[mid]<s,那么我们把a[mid]改成s ...
- NOI.AC 20181103 题解
CF 1037B Reach Median 班上 n个同学(n 是奇数)排成一排站队,为了美观,需要大家高度的中位数是 x. 你可以让同学们在脚下垫木板或者稍微蹲一点来达成这个目标.对任意一位同学的 ...
- 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 ...
- JMeter常见问题集合
前言 本文内容仅仅是针对Jmeter的部分功能名词的介绍和解释,以及初学者不易理解的问题的整理.部分内容来自别人做的整理,为了更好地整理自己的思路,所以可耻的整理一下发到博客上. 标题[1-6]和[参 ...
- JMeter 问题
1. JMeter 测试计划 测试计划 使用 JMeter 进行测试的起点,是其它 JMeter 测试元件的容器. 线程组 代表一定数量的并发用户,它可以用来模拟并发用户发送请求.实际的请求内容在S ...
- R语言-数据高级管理
数学函数 abs() 绝对值 sqrt() 平方 ceiling() 向上取整 floor() 向下取整 trunc() 截取整数部分 round(x,digits = n) 保留几位小数 统计函数 ...
- 【摘】 pt-query-digest工具一解
原文 http://blog.csdn.net/seteor/article/details/24017913 1.percona-toolkit安装 wget http://www.percona. ...
- Python数据分析之pandas学习
Python中的pandas模块进行数据分析. 接下来pandas介绍中将学习到如下8块内容:1.数据结构简介:DataFrame和Series2.数据索引index3.利用pandas查询数据4.利 ...
随机推荐
- laraval一键安装包的下载地址
http://laravelacademy.org/resources-download
- 循环语句中,break和continue分别有什么作用?
break用于强行退出循环,不执行循环中剩余的语句 continue用于跳过本次循环, 不执行continue后的语句, 继续下一次循环
- CentOS 关闭图形用户界面
1 centos 7以下版本 vim /etc/inittab :initdefault: #改为 :initdefault: 2. centos 7.x版本 rm -f /etc/systemd/s ...
- 对mysql事务提交、回滚的错误理解
一.起因 begin或者START TRANSACTION开始一个事务 rollback事务回滚 commit 事务确认 人们对事务的解释如下:事务由作为一个单独单元的一个或多个SQL语句组成,如果其 ...
- 《linux性能及调优指南》 3.4 硬盘瓶颈
翻译:Hank (http://blog.csdn.net/fireroll)版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明.原文名称:<Linux Performance an ...
- SurFS:共享式和分布式集群各取所长
http://www.ccidnet.com/2016/0811/10168835.shtml 一个集群系统可以做成三层定义,也就是后端存储访问层.沟通协作层.前端数据访问层,如果愣是要给每个层起个洋 ...
- Linux free -m 详解命令
如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. 1 2 3 4 5 6 $ free -m total used ...
- js语法规则 ---console.log ---- prompt ----基本类型 ---parseInt
在页面中可以在body里面加入type=”text/javascript” 例如: <script type="text/javascript"> </scrip ...
- JavaScript正则表达式以及字符串处理
正则表达式之基本概念 在我们写页面时,往往需要对表单的数据比如账号.身份证号等进行验证,而最有效的.用的最多的便是使用正则表达式来验证.那什么是正则表达式呢? 正则表达式(Regular Expres ...
- MySql 索引 查询 优化
官方文档: https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain_rows type: 连接类型 system 表只有 ...