CF2137D.Replace with Occurrences

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given an array aa, let f(x)f(x) be the number of occurrences of xx in the array aa. For example, when a=[1,2,3,1,4]a=[1,2,3,1,4], then f(1)=2f(1)=2 and f(3)=1f(3)=1.

You have an array bb of size nn. Please determine if there is an array aa of size nn such that f(ai)=bif(a_i)=b_i for all 1≤i≤n1 \leq i \leq n. If there is one, construct it.

给定一个数组 aa,令 f(x)f(x) 表示 xx 在数组 aa 中出现的次数。例如,当 a=[1,2,3,1,4]a=[1,2,3,1,4] 时,有 f(1)=2f(1)=2 且 f(3)=1f(3)=1。

你有一个长度为 nn 的数组 bb。请判断是否存在一个长度为 nn 的数组 aa,使得对所有 1≤i≤n1 \leq i \leq n 均满足 f(ai)=bif(a_i)=b_i。若存在,请构造出这样的数组 aa。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

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

The second line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n (1≤bi≤n1 \leq b_i \leq n).

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 \leq n \leq 2\cdot 10^5)。

第二行包含 nn 个整数 b1,b2,…,bnb_1,b_2,\ldots,b_n(1≤bi≤n1 \leq b_i \leq n)。

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

输出格式

For each test case, output −1-1 if there is no valid array aa.

Otherwise, print the array aa of nn integers on a new line. The elements should satisfy 1≤ai≤n1 \leq a_i \leq n.

对于每个测试用例,若不存在合法的数组 aa,则输出 −1-1。

否则,在新的一行中输出由 nn 个整数组成的数组 aa。其中各元素需满足 1≤ai≤n1 \leq a_i \leq n。

输入输出样例

  • 输入#1

    3
    4
    1 2 3 4
    6
    1 2 2 3 3 3
    6
    6 6 6 6 6 6

    输出#1

    -1
    4 5 5 6 6 6
    2 2 2 2 2 2

说明/提示

In the first test case, it can be shown that no array matches the requirement.

In the second test case, 44, 55, 66 appear 1,2,31,2,3 times respectively. Thus, the output array is correct as f(4),f(5),f(5),f(6),f(6),f(6)f(4),f(5),f(5),f(6),f(6),f(6) are 1,2,2,3,3,31,2,2,3,3,3 respectively.

在第一个测试用例中,可以证明不存在满足要求的数组。

在第二个测试用例中,44、55、66 分别出现 11、22、33 次。因此,输出数组是正确的,因为 f(4),f(5),f(5),f(6),f(6),f(6)f(4),f(5),f(5),f(6),f(6),f(6) 的值分别为 1,2,2,3,3,31,2,2,3,3,3。

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

首页