2191B.md (2710B)
1 Source: https://codeforces.com/problemset/problem/2191/B 2 3 B. MEX Reordering 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 integer array $a$ consisting of $n$ elements. Denote $f(l, r) = \operatorname{MEX}([a_l, a_{l + 1}, \ldots, a_r])$$^{\text{∗}}$. 22 23 Determine if there is a way to reorder the array $a$ such that for every $i$ ($1 \le i \le n - 1$), $f(1, i) \neq f(i + 1, n)$. In other words, for every split point $i$, the $\operatorname{MEX}$ of the prefix must be different from the $\operatorname{MEX}$ of the suffix. 24 25 $^{\text{∗}}$The minimum excluded (MEX) of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ which does not occur in the collection $c$. 26 27 Input 28 29 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows. 30 31 The first line of each test case contains a single integer $n$ ($2 \le n \le 100$) — the length of the array. 32 33 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le n$). 34 35 Output 36 37 Output "YES" if you can reorder $a$ so that the condition from the statement is satisfied, and "NO" otherwise. 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. 38 39 Example 40 41 Input 42 43 ``` 44 3 45 46 2 47 48 1 0 49 50 3 51 52 0 3 0 53 54 6 55 56 1 0 5 0 6 1 57 ``` 58 59 Output 60 61 ``` 62 YES 63 64 NO 65 66 YES 67 ``` 68 69 Note 70 71 In the first example, the initial ordering of $a$ already satisfies the condition. The only choice for $i$ is $i = 1$. Then $f(1, i) = f(1, 1) = \operatorname{MEX}([a_1]) = \operatorname{MEX}([1]) = 0$, and $f(i + 1, n) = f(2, 2) = \operatorname{MEX}([a_2]) = \operatorname{MEX}([0]) = 1$. Since $0 \neq 1$, the condition is satisfied. 72 73 In the second example, it can be shown that there is no way to reorder $a$ to satisfy the condition. As an example, consider the order $a = [3, 0, 0]$ and $i = 2$. We have $f(1, i) = f(1, 2) = \operatorname{MEX}([a_1, a_2]) = \operatorname{MEX}([3, 0]) = 1$, and $f(i + 1, n) = f(3, 3) = \operatorname{MEX}([a_3]) = \operatorname{MEX}([0]) = 1$, hence $f(1, i) = f(i + 1, n)$, so the choice of reordering is invalid. 74 75 In the third example, we can reorder $a$ into $[1, 6, 1, 0, 0, 5]$. When $i = 4$, $f(1, i) = f(1, 4) = \operatorname{MEX}([a_1, a_2, a_3, a_4]) = \operatorname{MEX}([1, 6, 1, 0]) = 2$, $f(i + 1, n) = f(5, 6) = \operatorname{MEX}([a_5, a_6]) = \operatorname{MEX}([0, 5]) = 1$, so the condition is satisfied for this $i$. It can be verified that the condition is also satisfied for all other $i$.