CF1738D.Permutation Addicts

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Given a permutation a1,a2,…,ana_1, a_2, \dots, a_n of integers from 11 to nn, and a threshold kk with 0≤k≤n0 \leq k \leq n, you compute a sequence b1,b2,…,bnb_1, b_2, \dots, b_n as follows.

For every 1≤i≤n1 \leq i \leq n in increasing order, let x=aix = a_i.

  • If x≤kx \leq k, set bxb_{x} to the last element aja_j (1≤j<i1 \leq j \lt i) that aj>ka_j \gt k. If no such element aja_j exists, set bx=n+1b_{x} = n+1.
  • If x>kx \gt k, set bxb_{x} to the last element aja_j (1≤j<i1 \leq j \lt i) that aj≤ka_j \leq k. If no such element aja_j exists, set bx=0b_{x} = 0.

Unfortunately, after the sequence b1,b2,…,bnb_1, b_2, \dots, b_n has been completely computed, the permutation a1,a2,…,ana_1, a_2, \dots, a_n and the threshold kk are discarded.

Now you only have the sequence b1,b2,…,bnb_1, b_2, \dots, b_n. Your task is to find any possible permutation a1,a2,…,ana_1, a_2, \dots, a_n and threshold kk that produce the sequence b1,b2,…,bnb_1, b_2, \dots, b_n. It is guaranteed that there exists at least one pair of permutation a1,a2,…,ana_1, a_2, \dots, a_n and threshold kk that produce the sequence b1,b2,…,bnb_1, b_2, \dots, b_n.

A permutation of integers from 11 to nn is a sequence of length nn which contains all integers from 11 to nn exactly once.

给定一个由 11 到 nn 的整数组成的排列 a1,a2,…,ana_1, a_2, \dots, a_n,以及一个阈值 kk(满足 0≤k≤n0 \leq k \leq n),我们按如下方式计算一个序列 b1,b2,…,bnb_1, b_2, \dots, b_n。

对每个 1≤i≤n1 \leq i \leq n(按递增顺序),令 x=aix = a_i:

  • 若 x≤kx \leq k,则将 bxb_{x} 设为最后一个满足 aj>ka_j > k 的元素 aja_j(其中 1≤j<i1 \leq j < i);若不存在这样的 aja_j,则令 bx=n+1b_{x} = n+1。
  • 若 x>kx > k,则将 bxb_{x} 设为最后一个满足 aj≤ka_j \leq k 的元素 aja_j(其中 1≤j<i1 \leq j < i);若不存在这样的 aja_j,则令 bx=0b_{x} = 0。

不幸的是,在序列 b1,b2,…,bnb_1, b_2, \dots, b_n 完全计算完毕后,原始排列 a1,a2,…,ana_1, a_2, \dots, a_n 和阈值 kk 均被丢弃。

现在你仅拥有序列 b1,b2,…,bnb_1, b_2, \dots, b_n。你的任务是找出任意一组可能的排列 a1,a2,…,ana_1, a_2, \dots, a_n 和阈值 kk,使得它们能生成该序列 b1,b2,…,bnb_1, b_2, \dots, b_n。题目保证至少存在一对排列 a1,a2,…,ana_1, a_2, \dots, a_n 和阈值 kk 能生成给定的序列 b1,b2,…,bnb_1, b_2, \dots, b_n。

由 11 到 nn 的整数构成的一个排列,是指一个长度为 nn 的序列,其中恰好包含 11 到 nn 的每一个整数各一次。

输入格式

Each test contains multiple test cases. The first line contains an integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The following lines contain the description of each test case.

The first line of each test case contains an integer nn (1≤n≤1051 \leq n \leq 10^5), indicating the length of the permutation aa.

The second line of each test case contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n (0≤bi≤n+10 \leq b_i \leq n+1), indicating the elements of the sequence bb.

It is guaranteed that there exists at least one pair of permutation a1,a2,…,ana_1, a_2, \dots, a_n and threshold kk that produce the sequence b1,b2,…,bnb_1, b_2, \dots, b_n.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。接下来的各行描述各个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5),表示排列 aa 的长度。

每个测试用例的第二行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \dots, b_n(0≤bi≤n+10 \leq b_i \leq n+1),表示序列 bb 的元素。

保证至少存在一对排列 a1,a2,…,ana_1, a_2, \dots, a_n 和阈值 kk,能生成序列 b1,b2,…,bnb_1, b_2, \dots, b_n。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, output the threshold kk (0≤k≤n0 \leq k \leq n) in the first line, and then output the permutation a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \leq a_i \leq n) in the second line such that the permutation a1,a2,…,ana_1, a_2, \dots, a_n and threshold kk produce the sequence b1,b2,…,bnb_1, b_2, \dots, b_n. If there are multiple solutions, you can output any of them.

对于每个测试用例,第一行输出阈值 kk(0≤k≤n0 \leq k \leq n),第二行输出排列 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \leq a_i \leq n),使得该排列 a1,a2,…,ana_1, a_2, \dots, a_n 与阈值 kk 共同生成序列 b1,b2,…,bnb_1, b_2, \dots, b_n。若存在多个解,输出任意一个即可。

输入输出样例

  • 输入#1

    3
    4
    5 3 1 2
    6
    7 7 7 3 3 3
    6
    4 4 4 0 0 0

    输出#1

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

说明/提示

For the first test case, permutation a=[1,3,2,4]a = [1,3,2,4] and threshold k=2k = 2 will produce sequence bb as follows.

  • When i=1i = 1, x=ai=1≤kx = a_i = 1 \leq k, there is no aja_j (1≤j<i1 \leq j \lt i) that aj>ka_j \gt k. Therefore, b1=n+1=5b_1 = n + 1 = 5.
  • When i=2i = 2, x=ai=3>kx = a_i = 3 \gt k, the last element aja_j that aj≤ka_j \leq k is a1a_1. Therefore, b3=a1=1b_3 = a_1 = 1.
  • When i=3i = 3, x=ai=2≤kx = a_i = 2 \leq k, the last element aja_j that aj>ka_j \gt k is a2a_2. Therefore, b2=a2=3b_2 = a_2 = 3.
  • When i=4i = 4, x=ai=4>kx = a_i = 4 \gt k, the last element aja_j that aj≤ka_j \leq k is a3a_3. Therefore, b4=a3=2b_4 = a_3 = 2.

Finally, we obtain sequence b=[5,3,1,2]b = [5,3,1,2].

For the second test case, permutation a=[1,2,3,4,5,6]a = [1,2,3,4,5,6] and threshold k=3k = 3 will produce sequence bb as follows.

  • When i=1,2,3i = 1, 2, 3, ai≤ka_i \leq k, there is no aja_j (1≤j<i1 \leq j \lt i) that aj>ka_j \gt k. Therefore, b1=b2=b3=n+1=7b_1 = b_2 = b_3 = n + 1 = 7.
  • When i=4,5,6i = 4, 5, 6, ai>ka_i \gt k, the last element aja_j that aj≤ka_j \leq k is a3a_3. Therefore, b4=b5=b6=a3=3b_4 = b_5 = b_6 = a_3 = 3.

Finally, we obtain sequence b=[7,7,7,3,3,3]b = [7,7,7,3,3,3].

For the third test case, permutation a=[6,5,4,3,2,1]a = [6,5,4,3,2,1] and threshold k=3k = 3 will produce sequence bb as follows.

  • When i=1,2,3i = 1, 2, 3, ai>ka_i \gt k, there is no aja_j (1≤j<i1 \leq j \lt i) that aj≤ka_j \leq k. Therefore, b4=b5=b6=0b_4 = b_5 = b_6 = 0.
  • When i=4,5,6i = 4, 5, 6, ai≤ka_i \leq k, the last element aja_j that aj>ka_j \gt k is a3a_3. Therefore, b1=b2=b3=a3=4b_1 = b_2 = b_3 = a_3 = 4.

Finally, we obtain sequence b=[4,4,4,0,0,0]b = [4,4,4,0,0,0].

对于第一个测试用例,排列 a=[1,3,2,4]a = [1,3,2,4] 与阈值 k=2k = 2 将生成序列 bb,过程如下:

  • 当 i=1i = 1 时,x=ai=1≤kx = a_i = 1 \leq k,不存在满足 1≤j<i1 \leq j \lt i 且 aj>ka_j \gt k 的 aja_j。因此,b1=n+1=5b_1 = n + 1 = 5。
  • 当 i=2i = 2 时,x=ai=3>kx = a_i = 3 \gt k,最后一个满足 aj≤ka_j \leq k 的 aja_j 是 a1a_1。因此,b3=a1=1b_3 = a_1 = 1。
  • 当 i=3i = 3 时,x=ai=2≤kx = a_i = 2 \leq k,最后一个满足 aj>ka_j \gt k 的 aja_j 是 a2a_2。因此,b2=a2=3b_2 = a_2 = 3。
  • 当 i=4i = 4 时,x=ai=4>kx = a_i = 4 \gt k,最后一个满足 aj≤ka_j \leq k 的 aja_j 是 a3a_3。因此,b4=a3=2b_4 = a_3 = 2。

最终,我们得到序列 b=[5,3,1,2]b = [5,3,1,2]。

对于第二个测试用例,排列 a=[1,2,3,4,5,6]a = [1,2,3,4,5,6] 与阈值 k=3k = 3 将生成序列 bb,过程如下:

  • 当 i=1,2,3i = 1, 2, 3 时,ai≤ka_i \leq k,不存在满足 1≤j<i1 \leq j \lt i 且 aj>ka_j \gt k 的 aja_j。因此,b1=b2=b3=n+1=7b_1 = b_2 = b_3 = n + 1 = 7。
  • 当 i=4,5,6i = 4, 5, 6 时,ai>ka_i \gt k,最后一个满足 aj≤ka_j \leq k 的 aja_j 是 a3a_3。因此,b4=b5=b6=a3=3b_4 = b_5 = b_6 = a_3 = 3。

最终,我们得到序列 b=[7,7,7,3,3,3]b = [7,7,7,3,3,3]。

对于第三个测试用例,排列 a=[6,5,4,3,2,1]a = [6,5,4,3,2,1] 与阈值 k=3k = 3 将生成序列 bb,过程如下:

  • 当 i=1,2,3i = 1, 2, 3 时,ai>ka_i \gt k,不存在满足 1≤j<i1 \leq j \lt i 且 aj≤ka_j \leq k 的 aja_j。因此,b4=b5=b6=0b_4 = b_5 = b_6 = 0。
  • 当 i=4,5,6i = 4, 5, 6 时,ai≤ka_i \leq k,最后一个满足 aj>ka_j \gt k 的 aja_j 是 a3a_3。因此,b1=b2=b3=a3=4b_1 = b_2 = b_3 = a_3 = 4。

最终,我们得到序列 b=[4,4,4,0,0,0]b = [4,4,4,0,0,0]。

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

首页