C. Median
Time limit per test2 seconds
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
A 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, limit 6, limit 1, limit 2, limit 3) is the number 2, and a median of array (0, limit 96, limit 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 wocould 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 shoshould 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 ≤ limit n ≤ limit 500, 1 ≤ limit x ≤ limit 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.
Question: Give You A number n and a number k, and then give you a series composed of n numbers, first sorted in non-descending order, then let you determine whether the Nth (n + 1)/2th of the series is k (the subscript of the series starts from 1). If not, you need to insert m numbers into this series so that the number (n + m + 1)/2 of the inserted series is k, and the minimum value of m is output.
Solution: first judge whether the number k is in the original series. If not, add the number k to the series, and then sort the original series, then find the first and last occurrence positions of the number k in the series, and calculate t = (n + 1)/2, if t> = first & t <= second, the result is output directly. If t <first, add a number greater than or equal to k after k; if t> second, the number before the number k must be smaller than or equal to the number of k.
Ps: This question may be subject to the brute force law. I am using a binary method, but there are many details to be aware. For details, see the code:
<SPAN style = "FONT-SIZE: 18px ">#include <iostream> # include <cstring> # include <string> # include <cstdio> # include <cmath> # include <algorithm> # include <queue> using namespace std; const int MAXN = 1e5 + 5; int vis [MAXN]; int s [MAXN]; bool cmp (int a, int B) {return a <B;} int main () {int n, k; while (scanf ("% d", & n, & k )! = EOF) {int I; int ans = 0; memset (vis, 0, sizeof (vis); for (I = 1; I <= n; I ++) {scanf ("% d", & s [I]); vis [s [I] ++;} if (vis [k] = 0) // if the number of columns does not contain k, add k {vis [k] ++; n ++; s [n] = k; ans ++ ;} sort (s + 1, s + n + 1, cmp); // because the subscript of the series starts from 1 ~ N sorting int t = (n + 1)> 1; int first =-1; int second =-1; int j; for (j = 1; j <= n; j ++) // records the position where k first appears and the position where it last appears {if (k = s [j]) {if (first =-1) first = j; second = j ;}} if (t >= first & t <= second) {printf ("% d \ n", ans) ;}// The following is a binary process, which is the essence of this program. Please carefully understand else if (t <first) {int r = n, l = 1, mid; while (r> l + 1) // note the bounce condition {mid = (r + l)> 1; int tmp = (mid + n + 1)> 1; If (first <tmp) {r = mid-1;} else if (first> tmp) {l = mid ;} else // make sure that {r = mid;} if (r = l + 1) can be determined separately when the conditions are equal. // This is also essential !! {If (l + n + 1)> 1 = first) {ans + = l;} else ans + = r; printf ("% d \ n", ans) ;}// the following process is true: Same as else {int r = n, l = 1, mid; while (r> l + 1) {mid = (r + l)> 1; int tmp = (n + mid + 1)> 1; if (second + mid <tmp) {l = mid + 1;} else if (second + mid = tmp) {r = mid;} else {r = mid-1 ;}} if (r = l + 1) {if (l + n + 1)> 1 = second + l) {ans + = l ;} else ans + = r;} else ans + = r; printf ("% d", ans) ;}} return 0 ;}</SPAN>