codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1037B.md (1720B)


      1 Source: https://codeforces.com/problemset/problem/1037/B
      2 
      3 B. Reach Median
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given an array $a$ of $n$ integers and an integer $s$. It is guaranteed that $n$ is odd.
     22 
     23 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 $s$.
     24 
     25 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, 8$ is equal to $6$, since if we sort this array we will get $5, 6, 8$, and $6$ is located on the middle position.
     26 
     27 Input
     28 
     29 The first line contains two integers $n$ and $s$ ($1\le n\le 2\cdot 10^5-1$, $1\le s\le 10^9$) — the length of the array and the required value of median.
     30 
     31 The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1\le a_i \le 10^9$) — the elements of the array $a$.
     32 
     33 It is guaranteed that $n$ is odd.
     34 
     35 Output
     36 
     37 In a single line output the minimum number of operations to make the median being equal to $s$.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 3 8  
     45 6 5 8
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 2
     52 ```
     53 
     54 Input
     55 
     56 ```
     57 7 20  
     58 21 15 12 11 20 19 12
     59 ```
     60 
     61 Output
     62 
     63 ```
     64 6
     65 ```
     66 
     67 Note
     68 
     69 In the first sample, $6$ can be increased twice. The array will transform to $8, 5, 8$, which becomes $5, 8, 8$ after sorting, hence the median is equal to $8$.
     70 
     71 In the second sample, $19$ can be increased once and $15$ can be increased five times. The array will become equal to $21, 20, 12, 11, 20, 20, 12$. If we sort this array we get $11, 12, 12, 20, 20, 20, 21$, this way the median is $20$.