zoukankan      html  css  js  c++  java
  • bjfu1208 中位数

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

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

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

    5 6
    1 2 2 4 5
    1 2 2 4 5 6 x x x x x
    
    5 5
    1 2 2 4 5
    1 2 2 4 5 x x x x
    
    5 5
    1 2 2 5 5
    1 2 2 5 5 x x
    
    5 4
    1 2 2 4 5
    1 2 2 4 5 x x
    
    5 3
    1 2 2 4 5
    1 2 2 3 4 5 x
    
    5 2
    1 2 2 4 5
    1 2 2 4 5
    
    9 2
    1 2 2 4 5 6 7 8 9
    x x x 1 2 2 4 5 6 7 8 9
    
    5 1
    1 2 2 4 5
    x x x 1 2 2 4 5

    代码如下:

    /*
     * 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 = 505;
    int data[maxn], n, x;
    int ans;
    
    void work() {
        int mid = (n - 1) / 2;
        if (data[mid] == x) {
            return ;
        }
        int lb = lower_bound(data, data + n, x) - data;
        int ub = upper_bound(data, data + n, x) - data - 1;
        if (ub < mid) {
            ans += n - 2 * ub - 2;
        } else {
            ans += 2 * lb - n + 1;
        }
    }
    
    int main() {
    #ifdef ON_LOCAL_DEBUG
        freopen("data.in", "r", stdin);
    #endif
        while (scanf("%d%d", &n, &x) == 2) {
            ans = 0;
            bool has = false;
            for (int i = 0; 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
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    timeDate.js 插件优化
    向页面中插入不同格式的时间(timeDate.js)
    html
    html
    html
    html
    three.js
    three.js
    three.js
    python之路_头像预览、each循环及form组件校验验证码
  • 原文地址:https://www.cnblogs.com/moonbay/p/4140342.html
Copyright © 2011-2022 走看看