CF2254E.Chronostasis

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Yousef has a hidden array aa of length nn consisting entirely of strictly positive integers.

An operation was performed exactly once to create an array bb:

  • Set b1=a1b_1 = a_1.
  • For every ii from 22 to nn, set bi=ai−ai−1b_i = a_i - a_{i-1}.
  • After this, the elements of bb were completely shuffled.

You are given the shuffled array bb. Reconstruct the lexicographically smallest original array aa. If it's impossible for any arrangement of bb to produce an array aa of strictly positive integers, output −1-1.

优素福有一个长度为 nn 的隐藏数组 aa,其中所有元素均为严格正整数。

恰好执行了一次如下操作来构造数组 bb:

  • 令 b1=a1b_1 = a_1;
  • 对每个从 22 到 nn 的 ii,令 bi=ai−ai−1b_i = a_i - a_{i-1};
  • 此后,数组 bb 的所有元素被完全打乱(即重排)。

你被给定打乱后的数组 bb。请重构出字典序最小的原始数组 aa。如果不存在任何 bb 的排列方式,使得由此生成的数组 aa 中所有元素均为严格正整数,则输出 −1-1。

输入格式

The first line of input contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the size of the array.

The second line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (−109≤bi≤109-10^9 \le b_i \le 10^9) — the elements of the shuffled array bb.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示数组的大小。

每个测试用例的第二行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(−109≤bi≤109-10^9 \le b_i \le 10^9)—— 表示被打乱顺序后的数组 bb 的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output nn strictly positive integers a1,a2,…,ana_1, a_2, \dots, a_n (ai≥1a_i \ge 1) — the lexicographically smallest original array aa. If it's impossible to create a valid array aa, output −1-1 instead.

对于每个测试用例,输出 nn 个严格正整数 a1,a2,…,ana_1, a_2, \dots, a_n(即 ai≥1a_i \ge 1)——字典序最小的原始数组 aa。如果无法构造出有效的数组 aa,则输出 −1-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]a = [5].

In the second test case, there is no valid arrangement of the elements of bb that reconstructs an array aa consisting entirely of strictly positive integers. Therefore, the answer is −1-1.

In the third test case, one valid arrangement reconstructs the array a=[1,1,3,2,6,3]a=[1,1,3,2,6,3]. The resulting sequence of differences [1,0,2,−1,4,−3][1, 0, 2, -1, 4, -3] is a permutation of the given array bb, and among all valid reconstructions, this array is lexicographically smallest.

在第一个测试用例中,唯一有效的数组是 a=[5]a = [5]。

在第二个测试用例中,不存在一种对数组 bb 元素的有效排列方式,能够重构出一个完全由严格正整数组成的数组 aa。因此答案为 −1-1。

在第三个测试用例中,一种有效的排列方式重构出数组 a=[1,1,3,2,6,3]a=[1,1,3,2,6,3]。所得的差分序列 [1,0,2,−1,4,−3][1, 0, 2, -1, 4, -3] 是给定数组 bb 的一个排列;而在所有有效的重构方案中,该数组是字典序最小的。

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

首页