欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1427A.Avoiding Zero
Avoiding Zero
You are given an array of integers .
You have to create an array of integers such that:
-
The array is a rearrangement of the array , that is, it contains the same values and each value appears the same number of times in the two arrays. In other words, the multisets and are equal.
For example, if , then and are rearrangements of , but and are not rearrangements of .
-
For all the sum of the first elements of is nonzero. Formally, for all , it must hold $$b_1+b_2+\cdots+b_k\not=0,.$$
If an array with the required properties does not exist, you have to print NO.
Input
Each test contains multiple test cases. The first line contains an integer () — the number of test cases. The description of the test cases follows.
The first line of each testcase contains one integer () — the length of the array .
The second line of each testcase contains integers () — the elements of .
Output
For each testcase, if there is not an array with the required properties, print a single line with the word NO.
Otherwise print a line with the word YES, followed by a line with the integers .
If there is more than one array satisfying the required properties, you can print any of them.
Note
Explanation of the first testcase: An array with the desired properties is . For this array, it holds:
- The first element of is .
- The sum of the first two elements of is .
- The sum of the first three elements of is .
- The sum of the first four elements of is .
Explanation of the second testcase: Since all values in are , any rearrangement of will have all elements equal to and therefore it clearly cannot satisfy the second property described in the statement (for example because ). Hence in this case the answer is NO.
Explanation of the third testcase: An array with the desired properties is . For this array, it holds:
- The first element of is .
- The sum of the first two elements of is .
- The sum of the first three elements of is .
- The sum of the first four elements of is .
- The sum of the first five elements of is .
Explanation of the fourth testcase: An array with the desired properties is . For this array, it holds:
- The first element of is .
- The sum of the first two elements of is .
- The sum of the first three elements of is .
- The sum of the first four elements of is .
- The sum of the first five elements of is .
- The sum of the first six elements of is .
Samples
4
4
1 -2 3 -4
3
0 0 0
5
1 -1 1 -1 1
6
40 -31 -9 0 13 -40
YES
1 -2 3 -4
NO
YES
1 1 -1 1 -1
YES
-40 13 40 0 -9 -31
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |