CF1685A.Circular Local MiniMax

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given nn integers a1,a2,…,ana_1, a_2, \ldots, a_n. Is it possible to arrange them on a circle so that each number is strictly greater than both its neighbors or strictly smaller than both its neighbors?

In other words, check if there exists a rearrangement b1,b2,…,bnb_1, b_2, \ldots, b_n of the integers a1,a2,…,ana_1, a_2, \ldots, a_n such that for each ii from 11 to nn at least one of the following conditions holds:

  • bi−1<bi>bi+1b_{i-1} \lt b_i \gt b_{i+1}
  • bi−1>bi<bi+1b_{i-1} \gt b_i \lt b_{i+1}

To make sense of the previous formulas for i=1i=1 and i=ni=n, one shall define b0=bnb_0=b_n and bn+1=b1b_{n+1}=b_1.

给你 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n。是否可以将它们排成一个圆圈,使得每个数都严格大于它的两个相邻数,或严格小于它的两个相邻数?

换句话说,请判断是否存在 a1,a2,…,ana_1, a_2, \ldots, a_n 的一个重排 b1,b2,…,bnb_1, b_2, \ldots, b_n,使得对每个 ii(从 11 到 nn),以下两个条件中至少有一个成立:

  • bi−1<bi>bi+1b_{i-1} \lt b_i \gt b_{i+1}
  • bi−1>bi<bi+1b_{i-1} \gt b_i \lt b_{i+1}

为使上述公式在 i=1i=1 和 i=ni=n 时有意义,定义 b0=bnb_0 = b_n 且 bn+1=b1b_{n+1} = b_1。

输入格式

The first line of the input contains a single integer tt (1≤t≤3⋅1041 \le t \le 3\cdot 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (3≤n≤1053 \le n \le 10^5) — the number of integers.

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

The sum of nn over all test cases doesn't exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤3⋅1041 \le t \le 3\cdot 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤1053 \le n \le 10^5),表示整数的个数。

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

所有测试用例中 nn 的总和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, if it is not possible to arrange the numbers on the circle satisfying the conditions from the statement, output NO\texttt{NO}. You can output each letter in any case.

Otherwise, output YES\texttt{YES}. In the second line, output nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n, which are a rearrangement of a1,a2,…,ana_1, a_2, \ldots, a_n and satisfy the conditions from the statement. If there are multiple valid ways to arrange the numbers, you can output any of them.

对于每个测试用例,若无法将数字按题目所述条件排列成一个环,则输出 NO\texttt{NO}。其中每个字母的大小写均可。

否则,输出 YES\texttt{YES}。在第二行输出 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n,它们是 a1,a2,…,ana_1, a_2, \ldots, a_n 的一个重排,且满足题目所述条件。若存在多种合法的排列方式,输出任意一种即可。

输入输出样例

  • 输入#1

    4
    3
    1 1 2
    4
    1 9 8 4
    4
    2 0 2 2
    6
    1 1 1 11 111 1111

    输出#1

    NO
    YES
    1 8 4 9 
    NO
    YES
    1 11 1 111 1 1111

说明/提示

It can be shown that there are no valid arrangements for the first and the third test cases.

In the second test case, the arrangement [1,8,4,9][1, 8, 4, 9] works. In this arrangement, 11 and 44 are both smaller than their neighbors, and 8,98, 9 are larger.

In the fourth test case, the arrangement [1,11,1,111,1,1111][1, 11, 1, 111, 1, 1111] works. In this arrangement, the three elements equal to 11 are smaller than their neighbors, while all other elements are larger than their neighbors.

可以证明,第一个和第三个测试用例不存在合法的排列。

在第二个测试用例中,排列 [1,8,4,9][1, 8, 4, 9] 是可行的。在此排列中,11 和 44 均小于其相邻元素,而 88 和 99 均大于其相邻元素。

在第四个测试用例中,排列 [1,11,1,111,1,1111][1, 11, 1, 111, 1, 1111] 是可行的。在此排列中,三个值为 11 的元素均小于其相邻元素,而所有其他元素均大于其相邻元素。

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

首页