D. Imbalanced Arrays

D. Imbalanced Arrays

Ntarsis has come up with an array a of n non-negative integers.

Call an array b of n integers imbalanced if it satisfies the following:

  • nbin, bi0,
  • there are no two indices (i,j) (1i,jn) such that bi+bj=0,
  • for each 1in, there are exactly ai indices j (1jn) such that bi+bj>0, where i and j are not necessarily distinct.

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

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t105). The description of the test cases follows.

The first line of each test case has a single integer n (1n105).

The next line contains n integers a1,a2,,an (0ain).

It is guaranteed that the sum of n across all test cases does not exceed 105.

Output

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

Otherwise, output "YES". Then, on the next line, output n integers b1,b2,,bn where bi0 for all 1in — an imbalanced array.

Example

input

复制代码
5
1
1
4
1 4 3 4
3
0 1 0
4
4 3 2 1
3
1 3 1
复制代码

output

复制代码
YES
1 
NO
YES
-3 1 -2 
YES
4 2 -1 -3 
YES
-1 3 -1
复制代码

Note

For the first test case, b=[1] is an imbalanced array. This is because for i=1, there is exactly one j (j=1) where b1+bj>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]. The array b=[3,1,2] is an imbalanced array.

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

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

 

解题思路

  首先从前两个条件可以知道我们只能从每一对(1,1),(2,2),(n,n)中选择一个数,比如选了1就不能再选1,但1可以重复选。

  如果存在合法的数组b,那么b一定存在一个绝对值最大的数,假设为bi。如果bi<0,那么一定有ai=0;否则如果bi>0,那么一定有ai=n。同时a中不可能同时存在ai=0aj=n,否则就会推出bi+bj<0bj+bi>0,就与第二个条件矛盾了。

  因此只要a中存在ai=0或者ai=n(不能同时存在),那么就在最大的数对中(即(n,n))取相应的元素赋值给bi,然后再把ai去掉,继续处理剩余的元素。

  为此可以先对a按照值的大小进行排序,然后设置两个指针lr分别表示a的最左端和最右端,然后判断是否满足上面所说的条件即可。其中如果去掉的最右端的ar,由于br>0,因此a中剩余的元素都要减去1,而删除al并不需要改变剩余元素的值。因此判断是否满足的条件就是(al(nr)=0)(ar(nr)=rl+1)的值是否为1,其中nr就是去掉右端的元素数量。

  AC代码如下,时间复杂度为O(nlogn)

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int N = 1e5 + 10;
 7 
 8 int a[N], p[N];
 9 int ans[N];
10 
11 void solve() {
12     int n;
13     scanf("%d", &n);
14     for (int i = 1; i <= n; i++) {
15         scanf("%d", a + i);
16         p[i] = i;
17     }
18     sort(p + 1, p + n + 1, [&](int i, int j) {
19         return a[i] < a[j];
20     });
21     for (int i = 1, j = n, k = n; i <= j; k--) {
22         if (!(a[p[i]] - (n - j)) ^ (a[p[j]] - (n - j) == j - i + 1)) {
23             if (!(a[p[i]] - (n - j))) ans[p[i++]] = -k;
24             else ans[p[j--]] = k;
25         }
26         else {
27             printf("NO\n");
28             return;
29         }
30     }
31     printf("YES\n");
32     for (int i = 1; i <= n; i++) {
33         printf("%d ", ans[i]);
34     }
35     printf("\n");
36 }
37 
38 int main() {
39     int t;
40     scanf("%d", &t);
41     while (t--) {
42         solve();
43     }
44     
45     return 0;
46 }
复制代码

 

参考资料

  Codeforces Round 887 (Div 1, Div 2) Tutorial:https://codeforces.com/blog/entry/116940

posted @   onlyblues  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示