题目是给你一个数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. POJ1328Radar Installation

    http://poj.org/problem?id=1328 题的大意就是说在海里有小岛,坐标位置会给出,需要岸边的雷达覆盖所有的小岛,但雷达的覆盖范围有限,所以,需要最少的雷达覆盖所有的小岛,但若是 ...

  2. keystonejs

    开始之前先确保你已经安装了Node.js 0.10+ 和MongoDB v2.4+. 要使用KeystoneJS,你需要掌握合理的Javascript知识,并熟悉数据库概念之类的基础知识,会用 nod ...

  3. 只有innoDB才允许使用外键

    1.只有InnoDB引擎才允许使用外键,所以,我们的数据表必须使用InnoDB引擎. 2.注意: 1.必须使用InnoDB引擎: 2.外键必须建立索引(INDEX): 3.外键绑定关系这里使用了“ O ...

  4. 1、创建一个JPA project(解决“at least one user library must be selected”问题)

    (注:本系列笔记是在学习尚硅谷JPA课程的时候写下的,结合课程内容和自我理解,方便自己以后进行复习) 一.在创建JPA之前看看什么是JPA 1.Java Persistence API:用于对象持久化 ...

  5. Maven是如何工作的

    Maven的依赖机制能够自动下载依赖文件,并且自动维护.更新版本. 让我们通过一个案例来了解Maven是如何工作的,假如你的系统想用log4j作为日志输出工具,让我们看一下传统做法和Maven做法: ...

  6. 今日思考:MVC系列框架之Struts存在的意义是什么?

    Struts其实就是MVC的代名词,那么提到MVC就不得不提早期的ModelⅠ.那时候JSP页面中混杂了大量的JAVA Scriptlet脚本语句,为了可维护性考虑,有人提出了ModelⅡ,也即现在的 ...

  7. chrome调试、移动端调试

    chrome 32版本后,添加 DevTools for Mobile 插件就可远程手机调试 DevTools for Mobile插件安装https://support.google.com/chr ...

  8. km算法的个人理解

    首先相对于上个blog讲的匈牙利算法用于解决无权二分图的最佳匹配,km算法则是在匈牙利算法基础上更进一层的,每条边增加了权值后,真的开始看时有些无厘头,觉得没有什么好方法,但两位牛人Kuhn-Munk ...

  9. IIS 10.0 无法安装 URL rewrite重写模块 2.0解决办法

    [问题描述]系统升级到Windows10后,IIS是10.0的,发现无法安装 URLRewrite重写模块 2.0. [解决办法]打开注册表编辑器,在HKEY_LOCAL_MACHINE\SOFTWA ...

  10. Discuz 5.x/6.x/7.x投票SQL注入分析

    看乌云有人爆了这个漏洞:http://www.wooyun.org/bugs/wooyun-2014-071516感觉应该是editpost.inc.php里投票的漏洞.因为dz已经确定不会再修补7. ...