#P10033. 「Cfz Round 3」Sum of Permutation

    ID: 11158 远端评测题 500ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学洛谷原创Special JudgeO2优化构造洛谷月赛Ad-hoc

「Cfz Round 3」Sum of Permutation

Problem Description

Please note the special time limit of this problem.

You are given a permutation pp of 1n1 \sim n.

You need to construct a sequence aa of length nn such that:

  • Every element in sequence aa is a positive integer not greater than nn.
  • There does not exist an ordered integer pair (l,r)(l,r) satisfying 1lrn1 \le l \le r \le n and i=lrai=i=lrpi\sum\limits_{i=l}^r a_i = \sum\limits_{i=l}^r p_i.

Or report that there is no solution.

A permutation of 1n1 \sim n means a sequence in which every positive integer not greater than nn appears exactly once.

Input Format

This problem has multiple test cases.

The first line contains an integer TT, indicating the number of test cases.

Then each test case is given as follows:

  • The first line contains an integer nn.
  • The second line contains nn integers, representing the given permutation pp.

Output Format

For each test case, output one line:

  • If there exists a sequence aa that satisfies the conditions, output nn integers separated by spaces, representing the sequence aa you constructed.
  • If no such sequence aa exists, output 1-1.

Any output that meets the requirements will be accepted.

4
3
3 2 1
2
1 2
5
4 2 1 5 3
7
5 7 3 1 2 4 6
1 3 3
-1
5 3 2 1 1
2 3 5 4 6 3 1

Hint

"Sample Explanation #1"

For the 11-st test case, both {1,3,3}\{1,3,3\} and {1,1,3}\{1,1,3\} are valid sequences aa.

For the 22-nd test case, it can be proven that no valid sequence aa exists.

For the 33-rd test case, besides {5,3,2,1,1}\{5,3,2,1,1 \}, {3,4,5,3,2}\{3,4,5,3,2 \}, {1,4,5,3,4}\{1,4,5,3,4 \}, {5,3,3,4,5}\{5,3,3,4,5\}, etc. are all valid sequences aa.

Constraints

Let n\sum n denote the sum of nn within a single test.

For all testdata, 1T50001 \le T \le 5000, 2n1062 \le n \le 10^6, n106\sum n \le 10^6. It is guaranteed that pp is a permutation of 1n1 \sim n.

You can get the score for this problem only if you pass all test points.

The input and output size is large, so please use fast I/O.

Translated by ChatGPT 5