CodeForces 166C

来源:互联网 发布:数据质量盘点标准 编辑:程序博客网 时间:2024/05/22 17:18
C. Median
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

median in an array with the length of n is an element which occupies position number  after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2, 6, 1, 2, 3) is the number 2, and a median of array (0, 96, 17, 23) — the number 17.

We define an expression  as the integer part of dividing number a by number b.

One day Vasya showed Petya an array consisting of n integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals x. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to x.

Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array.

While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need.

Input

The first input line contains two space-separated integers n and x (1 ≤ n ≤ 5001 ≤ x ≤ 105) — the initial array's length and the required median's value. The second line contains n space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different.

Output

Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals x.

Examples
input
3 1010 20 30
output
1
input
3 41 2 3
output
4
Note

In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position , that is, 10.

In the second sample you should add numbers 4555. The resulting array has median equal to 4.

题意:给定一个数字x,和已知的几个数,问需要添加多少个数字使得所给的数字x成为中位数,这里有一个不同的是当最终形成的数组的元素个数是偶数个时候,中位数是第2/n个数字。

思路:思路题,想好每一种情况就可以了。中位数左边的数的个数l和右边的数的个数r相等,或者l == r-1的时候也可以。按照这个思路来进行情况的判断。这样弄出来以后还不行,因为第一组样例不过,后来想到了,如果里面有y个x,情况就又不一样,于是情况的判断变成了,有0个x的时候,1个x的时候,多个x的时候。附上代码

#include<bits/stdc++.h>using namespace std;int num[1000];int main(){    int n,x;    scanf("%d%d",&n,&x);    int l = 0,r = 0,sam = 0;    for(int i = 1;i <= n;i++)    {        int m;scanf("%d",&m);        if(m == x)            sam++;        else if(m > x)            r++;        else            l++;    }    int ans = 0;    if(sam == 0)    {        if(abs(r - l)<=1&&r>l)            printf("1\n");        else if(r - l>1)            printf("%d\n",r - l);        else        {            printf("%d\n",l-r+1);        }    }    else if(sam == 1)    {        if(abs(r - l)<=1&&r > l)            printf("0\n");        else if(r - l>1)            printf("%d\n",r - l - 1);        else            printf("%d\n",l - r);    }    else    {        if(l + sam < r)            printf("%d\n",r-l-sam);        else if(l+sam >= r&&l < sam + r)            printf("0\n");        else            printf("%d\n",l - r- sam+1);    }}


0 0
原创粉丝点击