CF1852B.Imbalanced Arrays

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Ntarsis has come up with an array aa of nn non-negative integers.

Call an array bb of nn integers imbalanced if it satisfies the following:

  • −n≤bi≤n-n\le b_i\le n, bi≠0b_i \ne 0,
  • there are no two indices (i,j)(i, j) (1≤i,j≤n1 \le i, j \le n) such that bi+bj=0b_i + b_j = 0,
  • for each 1≤i≤n1 \leq i \leq n, there are exactly aia_i indices jj (1≤j≤n1 \le j \le n) such that bi+bj>0b_i+b_j \gt 0, where ii and jj are not necessarily distinct.

Given the array aa, Ntarsis wants you to construct some imbalanced array. Help him solve this task, or determine it is impossible.

Ntarsis 构造了一个由 nn 个非负整数组成的数组 aa。

称一个由 nn 个整数组成的数组 bb 是失衡的(imbalanced),当且仅当它满足以下条件:

  • 对每个 ii,有 −n≤bi≤n-n \le b_i \le n,且 bi≠0b_i \ne 0;
  • 不存在任意两个下标 (i,j)(i, j)(其中 1≤i,j≤n1 \le i, j \le n),使得 bi+bj=0b_i + b_j = 0;
  • 对每个 1≤i≤n1 \leq i \leq n,恰好存在 aia_i 个下标 jj(其中 1≤j≤n1 \le j \le n),使得 bi+bj>0b_i + b_j > 0(注意:此处 ii 与 jj 可以相同)。

给定数组 aa,Ntarsis 希望你构造出一个失衡数组 bb;若无法构造,请判断该任务不可能完成。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1051 \le t \le 10^5). The description of the test cases follows.

The first line of each test case has a single integer nn (1≤n≤1051 \leq n \leq 10^5).

The next line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai≤n0 \leq a_i \leq n).

It is guaranteed that the sum of nn across all test cases does not exceed 10510^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)。

下一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai≤n0 \leq a_i \leq n)。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, output "NO" if there exists no imbalanced array.

Otherwise, output "YES". Then, on the next line, output nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n where bi≠0b_i \neq 0 for all 1≤i≤n1 \leq i \leq n — an imbalanced array.

对于每个测试用例,若不存在不平衡数组,则输出 “NO”。

否则,输出 “YES”。然后在下一行输出 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n,其中对所有 1≤i≤n1 \leq i \leq n 均满足 bi≠0b_i \neq 0 —— 即一个不平衡数组。

输入输出样例

  • 输入#1

    5
    1
    1
    4
    1 4 3 4
    3
    0 1 0
    4
    4 3 2 1
    3
    1 3 1

    输出#1

    YES
    1 
    NO
    YES
    -3 1 -2 
    YES
    4 2 -1 -3 
    YES
    -1 3 -1

说明/提示

For the first test case, b=[1]b = [1] is an imbalanced array. This is because for i=1i = 1, there is exactly one jj (j=1j = 1) where b1+bj>0b_1 + b_j \gt 0.

For the second test case, it can be shown that there exists no imbalanced array.

For the third test case, a=[0,1,0]a = [0, 1, 0]. The array b=[−3,1,−2]b = [-3, 1, -2] is an imbalanced array.

  • For i=1i = 1 and i=3i = 3, there exists no index jj such that bi+bj>0b_i + b_j \gt 0.
  • For i=2i = 2, there is only one index j=2j = 2 such that bi+bj>0b_i + b_j \gt 0 (b2+b2=1+1=2b_2 + b_2 = 1 + 1 = 2).

Another possible output for the third test case could be b=[−2,1,−3]b = [-2, 1, -3].

对于第一个测试用例,b=[1]b = [1] 是一个不平衡数组。这是因为当 i=1i = 1 时,恰好存在一个 jj(即 j=1j = 1),使得 b1+bj>0b_1 + b_j \gt 0。

对于第二个测试用例,可以证明不存在不平衡数组。

对于第三个测试用例,a=[0,1,0]a = [0, 1, 0]。数组 b=[−3,1,−2]b = [-3, 1, -2] 是一个不平衡数组。

  • 当 i=1i = 1 和 i=3i = 3 时,不存在任何下标 jj,使得 bi+bj>0b_i + b_j \gt 0。
  • 当 i=2i = 2 时,仅存在一个下标 j=2j = 2,使得 bi+bj>0b_i + b_j \gt 0(即 b2+b2=1+1=2b_2 + b_2 = 1 + 1 = 2)。

第三个测试用例的另一个可能输出是 b=[−2,1,−3]b = [-2, 1, -3]。

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

首页