codeforces-scrape

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

2103C.md (3745B)


      1 Source: https://codeforces.com/problemset/problem/2103/C
      2 
      3 C. Median Splits
      4 
      5 time limit per test
      6 
      7 2 seconds
      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 The median of an array $b_1, b_2, \ldots b_m$, written as $\operatorname{med}(b_1, b_2, \ldots, b_m)$, is the $\left\lceil \frac{m}{2} \right\rceil$-th$^{\text{∗}}$ smallest element of array $b$.
     22 
     23 You are given an array of integers $a_1, a_2, \ldots, a_n$ and an integer $k$. You need to determine whether there exists a pair of indices $1 \le l \lt r \lt n$ such that:
     24 
     25 $$\operatorname{med}(\operatorname{med}(a_1, a_2, \ldots, a_l), \operatorname{med}(a_{l+1}, a_{l+2}, \ldots, a_r), \operatorname{med}(a_{r+1}, a_{r+2}, \ldots, a_n)) \le k.$$
     26 
     27 In other words, determine whether it is possible to split the array into three contiguous subarrays$^{\text{†}}$ such that the median of the three subarray medians is less than or equal to $k$.
     28 
     29 $^{\text{∗}}$$\lceil x \rceil$ is the ceiling function which returns the least integer greater than or equal to $x$.
     30 
     31 $^{\text{†}}$An array $x$ is a subarray of an array $y$ if $x$ can be obtained from $y$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
     32 
     33 Input
     34 
     35 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     36 
     37 The first line of each test case contains two integers $n$ and $k$ ($3 \le n \le 2 \cdot 10^5$, $1 \le k \le 10^9$) — the length of the array $a$ and the constant $k$.
     38 
     39 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.
     40 
     41 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     42 
     43 Output
     44 
     45 For each testcase, output "YES" if such a split exists, and "NO" otherwise.
     46 
     47 You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 6
     55 
     56 3 2
     57 
     58 3 2 1
     59 
     60 3 1
     61 
     62 3 2 1
     63 
     64 6 3
     65 
     66 8 5 3 1 6 4
     67 
     68 8 7
     69 
     70 10 7 12 16 3 15 6 11
     71 
     72 6 8
     73 
     74 7 11 12 4 9 17
     75 
     76 3 500000000
     77 
     78 1000 1000000000 1000
     79 ```
     80 
     81 Output
     82 
     83 ```
     84 YES
     85 NO
     86 NO
     87 YES
     88 YES
     89 YES
     90 ```
     91 
     92 Note
     93 
     94 In the first and second test case, the only possible partition of the array into three contiguous subarrays is $[3]$, $[2]$, $[1]$. Their respective medians are $3$, $2$, and $1$. The median of the three subarray medians is $\operatorname{med}(3, 2, 1) = 2$. Therefore, the answer for the first test case is "YES" since $2\le 2$, while the answer for the second test case is "NO" since $2 \gt 1$.
     95 
     96 In the third test case, it can be proven that no partition satisfies the constraints.
     97 
     98 In the fourth test case, one of the partitions satisfying the constraints is $[10, 7]$, $[12, 16, 3, 15]$, $[6, 11]$. The respective medians of subarrays are $7$, $12$, and $6$. The median of the three subarray medians is $\operatorname{med}(7, 12, 6) = 7 \le k$, hence this partition satisfies the constraints.
     99 
    100 In the fifth test case, one of the partitions satisfying the constraints is $[7, 11]$, $[12, 4]$, $[9, 17]$. The respective medians of the subarrays are $7$, $4$, and $9$. The median of the three subarray medians is $\operatorname{med}(7, 4, 9) = 7 \le k$, hence this partition satisfies the constraints.
    101 
    102 In the sixth test case, the only possible partition of the array into three contiguous subarrays is $[1000]$, $[10^9]$, $[1000]$. The respective medians of the subarrays are $1000$, $10^9$, and $1000$. The median of the three subarray medians is $\operatorname{med}(1000, 10^9, 1000) = 1000 \le k$, hence this partition satisfies the constraints.