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.利 ...
随机推荐
- 事件之Touch 事件的分发和消费机制
Android 中与 Touch 事件相关的方法包括:dispatchTouchEvent(MotionEvent ev).onInterceptTouchEvent(MotionEvent ev). ...
- Jmeter(二十五)Jmeter之系统函数
都忘了Jmeter4.0已发布((*^▽^*))具体优化项还没体验,记录一下,传送门:http://jmeter.apache.org/download_jmeter.cgi Jmeter的系统函数已 ...
- T-SQL 无参数的存储过程的创建和执行
use StudentManager go if exists(select * from sysobjects where name='usp_ScoreQuery') drop procedure ...
- ArcMap VBA实现连续编号
连续编号VBA部分:Static lCount as longlCount=lCount+1赋值部分:lCount (从1开始)lCount-1 (从0开始)
- KPPW2.2 漏洞利用--文件下载
KPPW2.2 漏洞利用--文件下载 任意文件下载漏洞 环境搭建 1,集成环境简单方便,如wamp,phpstudy.... 2,KPPW v2.2源码一份(文末有分享)放到WWW目录下面 3,安装, ...
- system调用导致子进程socket句柄泄漏问题分析
问题引出:A进程与B进程各自独立,都是服务器进程,常驻系统,互不相干.在某次重启A进程后,发现由于固定监听的端口被占用而无法启动.检查,发现是B进程占用了该端口,检查B进程代码,没有相关的打开该固定端 ...
- Spring MVC 类型转换
SpringMVC类型转换: 1 日期类型转换: private Date birthday; <label for="">生日:<input type=&quo ...
- python基础知识-(1)语法基础知识总结(转载)
1.Python标识符 在 Python 里,标识符有字母.数字.下划线组成. 在 Python 中,所有标识符可以包括英文.数字以及下划线(_),但不能以数字开头. Python 中的标识符是区分大 ...
- scrapy框架之CrawlSpider操作
提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二:基 ...
- Synchronized常用用法
我们都知道 Synchronized 是线程安全同步用的,大部分程序可能只会用到同步方法上面.其实 Synchronized 可以用到更多的场合,栈长列举了以下几个用法. 1.同步普通方法 这个也是我 ...