• 1037B--Reach Median(中位数)


    median 中位数 odd 奇数 even 奇数

    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.

    Input

    The first line contains two integers nn and ss (1n210511≤n≤2⋅105−1, 1s1091≤s≤109) — the length of the array and the required value of median.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the elements of the array aa.

    It is guaranteed that nn is odd.

    Output

    In a single line output the minimum number of operations to make the median being equal to ss.

    Examples
    input
    Copy
    3 8
    6 5 8
    output
    Copy
    2
    input
    Copy
    7 20
    21 15 12 11 20 19 12
    output
    Copy
    6
    Note

    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.

    总之,要使中位数之前的都小于等于中位数,中位数之后的都大于等于中位数。

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int main()
     5 {
     6     long long n,s,a[200005];
     7     while(~scanf("%lld %lld",&n,&s))
     8     {
     9         for(int i=1;i<=n;i++)
    10             scanf("%lld",&a[i]);
    11         sort(a+1,a+1+n);
    12         long long sum=0;
    13         for(int i=n;i>=n/2+1;i--)
    14         {
    15             if(a[i]<s)
    16                 sum+=s-a[i];
    17             if(i==n/2+1&&a[i]>s)
    18                 sum+=a[i]-s;
    19         }
    20         for(int i=n/2;i>=1;i--)
    21         {
    22             if(a[i]>s)
    23                 sum+=a[i]-s;
    24         }
    25         printf("%lld
    ",sum);
    26     }
    27     return 0;
    28 }
  • 相关阅读:
    day67——前后端传输数据的编码格式、ajax传json数据/传文件、批量插入
    day66——choices参数、MTV/MVC模型、三种创建多对多的方式、AJAX
    day65——聚合函数、分组查询、F与Q查询、django开事务、orm查询优化
    dayⅢ、基本数据类型+运算符作业
    dayⅡ:编程语言+变量+垃圾回收制
    dayⅡ:变量作业
    dayⅠ:计算机基础知识
    ⅩⅥ:无参装饰器
    ⅩⅤ:作业
    ⅩⅤ:名称空间与作用域
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/9715220.html
Copyright © 2020-2023  润新知