CF2254E.Chronostasis
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Yousef has a hidden array a of length n consisting entirely of strictly positive integers.
An operation was performed exactly once to create an array b:
- Set b1=a1.
- For every i from 2 to n, set bi=ai−ai−1.
- After this, the elements of b were completely shuffled.
You are given the shuffled array b. Reconstruct the lexicographically smallest original array a. If it's impossible for any arrangement of b to produce an array a of strictly positive integers, output −1.
优素福有一个长度为 n 的隐藏数组 a,其中所有元素均为严格正整数。
恰好执行了一次如下操作来构造数组 b:
- 令 b1=a1;
- 对每个从 2 到 n 的 i,令 bi=ai−ai−1;
- 此后,数组 b 的所有元素被完全打乱(即重排)。
你被给定打乱后的数组 b。请重构出字典序最小的原始数组 a。如果不存在任何 b 的排列方式,使得由此生成的数组 a 中所有元素均为严格正整数,则输出 −1。
输入格式
The first line of input contains an integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the size of the array.
The second line of each test case contains n integers b1,b2,…,bn (−109≤bi≤109) — the elements of the shuffled array b.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤104)—— 表示测试用例的数量。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)—— 表示数组的大小。
每个测试用例的第二行包含 n 个整数 b1,b2,…,bn(−109≤bi≤109)—— 表示被打乱顺序后的数组 b 的元素。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case, output n strictly positive integers a1,a2,…,an (ai≥1) — the lexicographically smallest original array a. If it's impossible to create a valid array a, output −1 instead.
对于每个测试用例,输出 n 个严格正整数 a1,a2,…,an(即 ai≥1)——字典序最小的原始数组 a。如果无法构造出有效的数组 a,则输出 −1。
输入输出样例
输入#1
8 1 5 4 -5 2 1 1 6 -3 4 2 -1 1 0 6 -2 -2 4 1 0 1 7 0 0 -2 3 0 -1 2 8 -1 -1 -1 -1 5 0 0 1 5 1000000000 500000000 750000000 100000000 900000000 10 1000000000 -1000000000 500000000 -500000000 1 1 -1 -1 2 -2
输出#1
5 -1 1 1 3 2 6 3 1 1 2 6 4 2 2 1 1 1 1 4 2 1 1 1 6 5 4 3 2 100000000 600000000 1350000000 2250000000 3250000000 -1
说明/提示
In the first test case, the only valid array is a=[5].
In the second test case, there is no valid arrangement of the elements of b that reconstructs an array a consisting entirely of strictly positive integers. Therefore, the answer is −1.
In the third test case, one valid arrangement reconstructs the array a=[1,1,3,2,6,3]. The resulting sequence of differences [1,0,2,−1,4,−3] is a permutation of the given array b, and among all valid reconstructions, this array is lexicographically smallest.
在第一个测试用例中,唯一有效的数组是 a=[5]。
在第二个测试用例中,不存在一种对数组 b 元素的有效排列方式,能够重构出一个完全由严格正整数组成的数组 a。因此答案为 −1。
在第三个测试用例中,一种有效的排列方式重构出数组 a=[1,1,3,2,6,3]。所得的差分序列 [1,0,2,−1,4,−3] 是给定数组 b 的一个排列;而在所有有效的重构方案中,该数组是字典序最小的。
输入解题思路,AI测评打分。不知道怎么写?