题目是给你一个数x以及一个长度为n的数列,让你往数列里插入y个数,使数列的中位数正好是x,求y的最小值。(其实这题的中位数跟数学里的中位数有一点区别,略去不提)

那么就排完序以后分情况讨论一下就好了。

具体公式我就不推了,很简单的。这里附上几组我推公式时用到的测试数据(每组三行,前两行是题目的输入,第三行表示添加方法,x表示添加的数)


      x x x x x

     x x x x

     x x

     x x

      x

x x x         

x x x     

代码如下:

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
#ifdef ON_LOCAL_DEBUG
#else
#endif
const int maxn = ;
int data[maxn], n, x;
int ans; void work() {
int mid = (n - ) / ;
if (data[mid] == x) {
return ;
}
int lb = lower_bound(data, data + n, x) - data;
int ub = upper_bound(data, data + n, x) - data - ;
if (ub < mid) {
ans += n - * ub - ;
} else {
ans += * lb - n + ;
}
} int main() {
#ifdef ON_LOCAL_DEBUG
freopen("data.in", "r", stdin);
#endif
while (scanf("%d%d", &n, &x) == ) {
ans = ;
bool has = false;
for (int i = ; i < n; i++) {
scanf("%d", &data[i]);
has = has || (data[i] == x);
}
if (!has) {
data[n++] = x;
ans++;
}
sort(data, data + n);
work();
printf("%d\n", ans);
}
return ;
}

bjfu1208 中位数的更多相关文章

  1. [LeetCode] Find Median from Data Stream 找出数据流的中位数

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

  2. [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. BZOJ1303 [CQOI2009]中位数图

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. 在MySQL中,如何计算一组数据的中位数?

    要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...

  5. AC日记——中位数 洛谷 P1168

    题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...

  6. [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)

    题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...

  7. LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1

    思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...

  8. BZOJ 1303 CQOI2009 中位数图 水题

    1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Statu ...

  9. 【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays

    一道非常经典的题目,Median of Two Sorted Arrays.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/ ...

随机推荐

  1. hdu 4315 Climbing the Hill 博弈论

    题意:有n个人爬山,山顶坐标为0,其他人按升序给出,不同的坐标只能容纳一个人(山顶不限),Alice和Bob轮流选择一个人让他移动任意步,但不能越过前面的人,且不能和前面一个人在相同的位置.现在有一个 ...

  2. H5 移动Web框架集合

    http://frozenui.github.io/  一个简洁的h5前端框架 http://weui.github.io/weui/ 腾讯出的WebUI 风格是基于微信

  3. 【mysql的编程专题①】流程控制与其他语法

    流程控制与内置函数,一般用在select的field字段上,或者用在函数,存储过程,触发器中; 如果用在select上就会随着query出来的row来隐式迭代; 注释与语句结束符 语句结束符 默认有两 ...

  4. Debug过程中的mock (及display窗口的使用)

    转载:http://m.blog.csdn.net/blog/u012516903/18004965 在debug的时候,有3个地方可以进行mock测试 测试代码如下: 1.使用display窗口 W ...

  5. 老韩思考:一个卖豆腐的能转行IT吗? 你的卖点在哪里?

    前言: 我带过的学生很多,各行各业都有,泰牛程序员招生消息放出去后,还有一个在菜市场上卖豆腐的也看我的视频教程,决定转换IT行业,我想,北大毕业的可以卖猪肉,那么卖豆腐的为什么就不能从事IT行业呢?那 ...

  6. ArcEngine查询、添加、删除要数的方法

    原文地址:http://www.cnblogs.com/caodajieup/archive/2011/11/02/2232658.html 1.查找数据 1).利用FeaturCursor进行空间查 ...

  7. UVa 1301 - Fishnet

    求出所有交点枚举每个四边形找最大面积即可. #include <cstdio> #include <cmath> #include <algorithm> usin ...

  8. linux系统的文件类型学习

    linux是一个文件型操作系统,在linux下一切皆文件. 目录.字符设备.块设备.管道.套接字.符号连接文件等在linux下统统都是文件. linux下的文件类型分为以下几种类型: 1. 正规文件, ...

  9. [Codeforces677C]Vanya and Label(组合数学,快速幂)

    题目链接:http://codeforces.com/contest/677/problem/C 题意:给一个字符和数字的映射关系,然后再给一个字符串.问有多少个其他的字符串,使得那些字符串之间相互操 ...

  10. 【转载】Java垃圾回收内存清理相关(虚拟机书第三章),GC日志的理解,CPU时间、墙钟时间的介绍

    主要看<深入理解Java虚拟机> 第三张 P84 开始是垃圾收集相关. 1. 1960年诞生于MIT的Lisp是第一门采用垃圾回收的语言. 2. 程序计数器.虚拟机栈.本地方法栈3个区域随 ...