CF1798D.Shocking Arrangement

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array a1,a2,…,ana_1, a_2, \ldots, a_n consisting of integers such that a1+a2+…+an=0a_1 + a_2 + \ldots + a_n = 0.

You have to rearrange the elements of the array aa so that the following condition is satisfied:

\\max\\limits\_{1 \\le l \\le r \\le n} \\lvert a\_l + a\_{l+1} + \\ldots + a\_r \\rvert \\lt \\max(a\_1, a\_2, \\ldots, a\_n) - \\min(a\_1, a\_2, \\ldots, a\_n),$$ where $|x|$ denotes the absolute value of $x$. More formally, determine if there exists a permutation $p_1, p_2, \ldots, p_n$ that for the array $a_{p_1}, a_{p_2}, \ldots, a_{p_n}$, the condition above is satisfied, and find the corresponding array. Recall that the array $p_1, p_2, \ldots, p_n$ is called a permutation if for each integer $x$ from $1$ to $n$ there is exactly one $i$ from $1$ to $n$ such that $p_i = x$. 给你一个由整数构成的数组 $a_1, a_2, \ldots, a_n$,满足 $a_1 + a_2 + \ldots + a_n = 0$。 你需要重排数组 $a$ 的元素,使得以下条件成立: $$\max\limits_{1 \le l \le r \le n} \lvert a_l + a_{l+1} + \ldots + a_r \rvert \lt \max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n),

其中 ∣x∣|x| 表示 xx 的绝对值。

更形式化地说:判断是否存在一个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n,使得对重排后的数组 ap1,ap2,…,apna_{p_1}, a_{p_2}, \ldots, a_{p_n},上述条件成立;若存在,输出对应的数组。

注意:数组 p1,p2,…,pnp_1, p_2, \ldots, p_n 称为一个排列,当且仅当对每个从 11 到 nn 的整数 xx,恰好存在一个 i∈{1,2,…,n}i \in \{1,2,\ldots,n\} 满足 pi=xp_i = x。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤50 0001 \le t \le 50\,000). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤300 0001 \le n \le 300\,000) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−109≤ai≤109-10^9 \le a_i \le 10^9) — elements of the array aa. It is guaranteed that the sum of the array aa is zero, in other words: a1+a2+…+an=0a_1 + a_2 + \ldots + a_n = 0.

It is guaranteed that the sum of nn over all test cases does not exceed 300 000300\,000.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤300 0001 \le n \le 300\,000)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)—— 数组 aa 的元素。保证数组 aa 的元素和为零,即:a1+a2+…+an=0a_1 + a_2 + \ldots + a_n = 0。

保证所有测试用例的 nn 值之和不超过 300 000300\,000。

输出格式

For each test case, if it is impossible to rearrange the elements of the array aa in the required way, print "No" in a single line.

If possible, print "Yes" in the first line, and then in a separate line nn numbers — elements a1,a2,…,ana_1, a_2, \ldots, a_n rearranged in a valid order (ap1,ap2,…,apna_{p_1}, a_{p_2}, \ldots, a_{p_n}).

If there are several possible answers, you can output any of them.

对于每个测试用例,如果无法将数组 aa 的元素按要求重新排列,则在一行中输出“No”。

如果可以实现,则在第一行输出“Yes”,并在下一行输出 nn 个数——即按有效顺序重新排列后的元素 a1,a2,…,ana_1, a_2, \ldots, a_n(即 ap1,ap2,…,apna_{p_1}, a_{p_2}, \ldots, a_{p_n})。

如果有多种可能的答案,输出任意一种即可。

输入输出样例

  • 输入#1

    7
    4
    3 4 -2 -5
    5
    2 2 2 -3 -3
    8
    -3 -3 1 1 1 1 1 1
    3
    0 1 -1
    7
    -3 4 3 4 -4 -4 0
    1
    0
    7
    -18 13 -18 -17 12 15 13

    输出#1

    Yes
    -5 -2 3 4
    Yes
    -3 2 -3 2 2
    Yes
    1 1 1 -3 1 1 1 -3
    Yes
    -1 0 1
    Yes
    4 -4 4 -4 0 3 -3
    No
    Yes
    13 12 -18 15 -18 13 -17

说明/提示

In the first test case max⁡(a1,…,an)−min⁡(a1,…,an)=9\max(a_1, \ldots, a_n) - \min(a_1, \ldots, a_n) = 9. Therefore, the elements can be rearranged as [−5,−2,3,4][-5, -2, 3, 4]. It is easy to see that for such an arrangement ∣al+…+ar∣\lvert a_l + \ldots + a_r \rvert is always not greater than 77, and therefore less than 99.

In the second test case you can rearrange the elements of the array as [−3,2,−3,2,2][-3, 2, -3, 2, 2]. Then the maximum modulus of the sum will be reached on the subarray [−3,2,−3][-3, 2, -3], and will be equal to ∣−3+2+−3∣=∣−4∣=4\lvert -3 + 2 + -3 \rvert = \lvert -4 \rvert = 4, which is less than 55.

In the fourth test example, any rearrangement of the array aa will be suitable as an answer, including [−1,0,1][-1, 0, 1].

在第一个测试用例中,max⁡(a1,…,an)−min⁡(a1,…,an)=9\max(a_1, \ldots, a_n) - \min(a_1, \ldots, a_n) = 9。因此,这些元素可以重排为 [−5,−2,3,4][-5, -2, 3, 4]。容易看出,对于这种排列,任意子数组和的绝对值 ∣al+…+ar∣\lvert a_l + \ldots + a_r \rvert 始终不超过 77,因此小于 99。

在第二个测试用例中,你可以将数组元素重排为 [−3,2,−3,2,2][-3, 2, -3, 2, 2]。此时,子数组和的绝对值最大值在子数组 [−3,2,−3][-3, 2, -3] 上取得,其值为 ∣−3+2+−3∣=∣−4∣=4\lvert -3 + 2 + -3 \rvert = \lvert -4 \rvert = 4,该值小于 55。

在第四个测试样例中,数组 aa 的任意重排均满足要求,例如 [−1,0,1][-1, 0, 1]。

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

首页