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.利 ...
随机推荐
- 将OpenVZ系统硬盘从200G升级到400G
平时习惯性动作df了一下,结果发现...... ‘ 200G的磁盘空间快满了(99%),只剩下2G多了. 索性一下加到400G空间 目前为止很顺利. 但是OpenVZ这台需要关机再开机才行. 重新开机 ...
- 找出N个无序数中第K大的数
使用类似快速排序,执行一次快速排序后,每次只选择一部分继续执行快速排序,直到找到第K个大元素为止,此时这个元素在数组位置后面的元素即所求 时间复杂度: 1.若随机选取枢纽,线性期望时间O(N) 2.若 ...
- [UE4]不精准射击 Random Unit Vector in Cone in Radians
- [UE4]计算AimOffset偏移动画的角度
- 20165312 预备作业3 Linux安装及学习
Linux安装及学习 一.安装Vmware虚拟机以及Ubuntu中遇到的问题 因为之前安装过Vmware虚拟机,考虑到两者没有太大的差别,就所以就没有再安装Vbox虚拟机. 在安装Vmware虚拟机增 ...
- 使用CacheCloud管理Redis实例
转载来源:http://www.ywnds.com/?p=10610 一.CacheCloud是什么? 最近在使用CacheCloud管理Redis,所以简单说一下,这里主要说一下我碰到的问题.Cac ...
- 在线学习和在线凸优化(online learning and online convex optimization)—凸化方法4
一些在线预测问题可以转化到在线凸优化框架中.下面介绍两种凸化技术: 一些在线预测问题似乎不适合在线凸优化框架.例如,在线分类问题中,预测域(predictions domain)或损失函数不是凸的.我 ...
- 第11章 拾遗1:网络地址转换(NAT)和端口映射
1. 网络地址转换(NAT) 1.1 NAT的应用场景 (1)应用场景:允许将私有IP地址映射到公网地址,以减缓IP地址空间的消耗 ①需要连接Internet,但主机没有公网IP地址 ②更换了一个新的 ...
- centos 7怎么通过图形界面来配置静态ip
除了通过修改配置文件的方法来配置静态ip,我们还可以通过图形界面来配置,这样做其实更加方便一点 先进入设置页面 选择网络 我这里是通过有线上网的,我们之间修改配置就可以了 选择ipv4,和manual ...
- 内存大小设置 Java heap space错误
1. 问题描述 当从数据库中查询大量的数据,每个模板取出来几百万条数据,或者是频繁的刷新项目.模板时就会占用Java虚拟机JVM的大量内存,超过内存就会出现报java.lang.OutOfMemory ...