CF2191B.MEX Reordering

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an integer array aa consisting of nn elements. Denote f(l,r)=MEX⁡([al,al+1,…,ar])f(l, r) = \operatorname{MEX}([a_l, a_{l + 1}, \ldots, a_r])∗^{\text{∗}}.

Determine if there is a way to reorder the array aa such that for every ii (1≤i≤n−11 \le i \le n - 1), f(1,i)≠f(i+1,n)f(1, i) \neq f(i + 1, n). In other words, for every split point ii, the MEX⁡\operatorname{MEX} of the prefix must be different from the MEX⁡\operatorname{MEX} of the suffix.

∗^{\text{∗}}The minimum excluded (MEX) of a collection of integers c1,c2,…,ckc_1, c_2, \ldots, c_k is defined as the smallest non-negative integer xx which does not occur in the collection cc.

给你一个包含 nn 个元素的整数数组 aa。记 f(l,r)=MEX⁡([al,al+1,…,ar])f(l, r) = \operatorname{MEX}([a_l, a_{l + 1}, \ldots, a_r])∗^{\text{∗}}。

判断是否存在一种对数组 aa 的重排方式,使得对每个 ii(1≤i≤n−11 \le i \le n - 1),均有 f(1,i)≠f(i+1,n)f(1, i) \neq f(i + 1, n)。换言之,对每个分割点 ii,前缀的 MEX⁡\operatorname{MEX} 必须不等于后缀的 MEX⁡\operatorname{MEX}。

∗^{\text{∗}} 一个整数集合 c1,c2,…,ckc_1, c_2, \ldots, c_k 的**最小未出现值(MEX)**定义为不在该集合中出现的最小非负整数 xx。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤1002 \le n \le 100) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤n0 \le a_i \le n).

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤1002 \le n \le 100)—— 数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤n0 \le a_i \le n)。

输出格式

Output "YES" if you can reorder aa 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.

如果可以对 aa 进行重排,使得题目陈述中的条件得到满足,则输出 "YES";否则输出 "NO"。答案的大小写不限(即不区分大小写)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 均被视为肯定回答。

输入输出样例

  • 输入#1

    3
    2
    1 0
    3
    0 3 0
    6
    1 0 5 0 6 1

    输出#1

    YES
    NO
    YES

说明/提示

In the first example, the initial ordering of aa already satisfies the condition. The only choice for ii is i=1i = 1. Then f(1,i)=f(1,1)=MEX⁡([a1])=MEX⁡([1])=0f(1, i) = f(1, 1) = \operatorname{MEX}([a_1]) = \operatorname{MEX}([1]) = 0, and f(i+1,n)=f(2,2)=MEX⁡([a2])=MEX⁡([0])=1f(i + 1, n) = f(2, 2) = \operatorname{MEX}([a_2]) = \operatorname{MEX}([0]) = 1. Since 0≠10 \neq 1, the condition is satisfied.

In the second example, it can be shown that there is no way to reorder aa to satisfy the condition. As an example, consider the order a=[3,0,0]a = [3, 0, 0] and i=2i = 2. We have f(1,i)=f(1,2)=MEX⁡([a1,a2])=MEX⁡([3,0])=1f(1, i) = f(1, 2) = \operatorname{MEX}([a_1, a_2]) = \operatorname{MEX}([3, 0]) = 1, and f(i+1,n)=f(3,3)=MEX⁡([a3])=MEX⁡([0])=1f(i + 1, n) = f(3, 3) = \operatorname{MEX}([a_3]) = \operatorname{MEX}([0]) = 1, hence f(1,i)=f(i+1,n)f(1, i) = f(i + 1, n), so the choice of reordering is invalid.

In the third example, we can reorder aa into [1,6,1,0,0,5][1, 6, 1, 0, 0, 5]. When i=4i = 4, f(1,i)=f(1,4)=MEX⁡([a1,a2,a3,a4])=MEX⁡([1,6,1,0])=2f(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)=MEX⁡([a5,a6])=MEX⁡([0,5])=1f(i + 1, n) = f(5, 6) = \operatorname{MEX}([a_5, a_6]) = \operatorname{MEX}([0, 5]) = 1, so the condition is satisfied for this ii. It can be verified that the condition is also satisfied for all other ii.

在第一个例子中,数组 aa 的初始排列已满足条件。此时唯一可选的 ii 是 i=1i = 1。于是 f(1,i)=f(1,1)=MEX⁡([a1])=MEX⁡([1])=0f(1, i) = f(1, 1) = \operatorname{MEX}([a_1]) = \operatorname{MEX}([1]) = 0,而 f(i+1,n)=f(2,2)=MEX⁡([a2])=MEX⁡([0])=1f(i + 1, n) = f(2, 2) = \operatorname{MEX}([a_2]) = \operatorname{MEX}([0]) = 1。由于 0≠10 \neq 1,条件成立。

在第二个例子中,可以证明不存在对 aa 的重排方式使其满足条件。例如,考虑排列 a=[3,0,0]a = [3, 0, 0] 并取 i=2i = 2:此时 f(1,i)=f(1,2)=MEX⁡([a1,a2])=MEX⁡([3,0])=1f(1, i) = f(1, 2) = \operatorname{MEX}([a_1, a_2]) = \operatorname{MEX}([3, 0]) = 1,且 f(i+1,n)=f(3,3)=MEX⁡([a3])=MEX⁡([0])=1f(i + 1, n) = f(3, 3) = \operatorname{MEX}([a_3]) = \operatorname{MEX}([0]) = 1,因此 f(1,i)=f(i+1,n)f(1, i) = f(i + 1, n),该重排方案不合法。

在第三个例子中,可将 aa 重排为 [1,6,1,0,0,5][1, 6, 1, 0, 0, 5]。当 i=4i = 4 时,有 f(1,i)=f(1,4)=MEX⁡([a1,a2,a3,a4])=MEX⁡([1,6,1,0])=2f(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)=MEX⁡([a5,a6])=MEX⁡([0,5])=1f(i + 1, n) = f(5, 6) = \operatorname{MEX}([a_5, a_6]) = \operatorname{MEX}([0, 5]) = 1,故该 ii 满足条件。可以验证,该重排对所有其他 ii 值也均满足条件。

输入解题思路,AI测评打分。不知道怎么写?

首页