#P9054. [集训队互测 2022] 心跳排列图

    ID: 9956 远端评测题 1000ms 2048MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划 DP贪心集训队互测2022Special JudgeO2优化构造Ad-hoc分类讨论

[集训队互测 2022] 心跳排列图

Background

The distributed files can be found in the attachment.

Problem Description

Note: In this problem, all sequence indices start from 11.

What does the robot heartbeats look like when arranged into a graph?

You have a competitive programming robot that beats nn times per minute. The intensity of the ii-th beat is aia_i. Here, a1ana_1 \sim a_n is exactly a permutation of 1n1 \sim n.

Let AiA_i be the sequence obtained by deleting the ii-th element from sequence aa, i.e., Ai=[a1,,ai1,ai+1,,an]A_i = [a_1,\dots,a_{i-1},a_{i+1},\dots,a_n].

For a sequence pp with all distinct elements, let G(p)G(p) be an undirected graph with p|p| vertices, numbered 1p1 \sim |p|. For every pair of positive integers 1i<jp1 \le i \lt j \le |p|, if for all k[i,j]Zk \in [i,j] \cap \mathbb{Z}, we have pk[min(pi,pj),max(pi,pj)]p_k \in [\min(p_i,p_j), \max(p_i,p_j)], then there is an edge between vertex ii and vertex jj in G(p)G(p). Let F(p)F(p) be the length of the shortest path from vertex 11 to vertex p|p| in G(p)G(p), where the length of a path is defined as its number of edges.

Let f(a)=[F(A1),F(A2),,F(An)]f(a) = [F(A_1), F(A_2), \dots, F(A_n)].

Given a sequence [b1,,bn][b_1,\dots,b_n] of length nn, please find any permutation aa of 1n1 \sim n such that f(a)=bf(a) = b. It is guaranteed that a solution exists.

In some subtasks, the competitive programming robot little G will give you some “hints”. Let G0=G(a)G_0 = G(a). Let path0path_0 be the set of vertices visited by some shortest path from 11 to nn in G0G_0. Let pathjpath_j be the set of vertices visited by some shortest path from the start vertex to the end vertex in G(Aj)G(A_j) (note: for convenience, the vertex indices in pathjpath_j still use the indices from the original graph, see Sample 2). Then little G may additionally tell you all pathjpath_j (including path0path_0), or may only tell you path0path_0, or may provide no hints. See the input format for details.

It is guaranteed that the given hints are correct, i.e., there exists a permutation that satisfies all hints.

The distributed files include checker.cpp, which you can use to check whether your output is correct. Usage: ./checker input output output, where input and output are the input file and your output. testlib.h is also provided; please compile the checker with testlib.h placed in the same directory as the checker.

Input Format

The first line contains two positive integers: the subtask number SS and the number of test cases TT.

Then follow TT test cases. Each test case is:

  • The first line contains one positive integer nn.
  • The second line contains nn positive integers b1,,bnb_1,\dots,b_n.

In particular,

  1. If S=5S = 5, then for each test case, there will be an additional n+1n+1 lines. In these n+1n+1 lines, the ii-th line is a binary string cic_i of length nn, where ci,j=[jpathi1]c_{i,j} = [j \in path_{i-1}].
  2. If S=6S = 6, then for each test case, there will be a third line containing a binary string cc of length nn, where ci=[ipath0]c_i = [i \in path_0].

Notes:

  1. Even if your program does not need the hints, when S=5S = 5 or S=6S = 6 you still need to read the array cc.
  2. For the same input bb, the valid aa may not be unique, and thus cc may also not be unique. Your output is not required to satisfy the constraints given by our cc; it will be judged correct as long as it satisfies the constraints of bb.

Different variables on the same line are separated by a single space.

Output Format

For each test case, output one line with nn positive integers: the permutation aa you found.

9 11
4
2 2 1 1
4
2 2 2 2
4
2 1 1 2
7
5 5 4 4 4 5 5
7
1 3 2 2 2 2 4
7
3 3 2 4 4 5 3
8
2 2 3 5 3 3 3 4
8
5 4 4 4 4 6 6 5
8
4 4 4 2 4 4 2 3
9
4 7 5 5 5 5 3 4 4
9
3 4 4 4 4 4 4 4 6
1 2 4 3
2 1 4 3
1 3 2 4
3 1 7 2 6 4 5
3 1 6 4 2 5 7
2 3 1 6 4 7 5
5 6 3 1 7 4 2 8
1 8 2 7 3 5 6 4
6 3 2 7 4 5 1 8
5 8 6 3 7 1 9 2 4
2 9 3 1 8 5 7 6 4
5 1
4
2 2 1 1
1011
0111
1011
1001
1010
1 2 4 3
6 1
4
2 2 1 1
1011
1 2 4 3

Hint

Explanation of Sample 1

Consider the first test case in the sample. One solution is a=[1,2,4,3]a = [1,2,4,3]. Then A1,A2,A3,A4A_1, A_2, A_3, A_4 are [2,4,3][2,4,3], [1,4,3][1,4,3], [1,2,3][1,2,3], [1,2,4][1,2,4], respectively. The edges in the four graphs G(A1),G(A2),G(A3),G(A4)G(A_1), G(A_2), G(A_3), G(A_4) are:

  • G(A1)G(A_1): (1,2),(2,3)(1,2), (2,3). Therefore F(A1)=2F(A_1) = 2.
  • G(A2)G(A_2): (1,2),(2,3)(1,2), (2,3). Therefore F(A2)=2F(A_2) = 2.
  • G(A3)G(A_3): (1,2),(1,3),(2,3)(1,2), (1,3), (2,3). Therefore F(A3)=1F(A_3) = 1.
  • G(A4)G(A_4): (1,2),(1,3),(2,3)(1,2), (1,3), (2,3). Therefore F(A4)=1F(A_4) = 1.

So f(a)=[2,2,1,1]f(a) = [2,2,1,1], which matches the input.

The valid aa is not unique. For example, a=[4,3,1,2]a = [4,3,1,2] is also correct.

Explanation of Sample 2

The permutation in this sample is the same as the first test case in Sample 1, but this sample includes the hints for subtask 55. Note that when giving pathjpath_j, the original indices are still used. For example, after deleting 11, the vertices on the new shortest path are numbered 2342 \to 3 \to 4.

Explanation of Sample 3

The permutation in this sample is the same as the first test case in Sample 1, but this sample includes the hints for subtask 66.

Constraints

For all testdata: 1T4×1041 \le T \le 4 \times 10^4, 4n1054 \le n \le 10^5, n5×105\sum n \le 5 \times 10^5.

  • Subtask 1 (77 points): T250T \le 250, n7n \le 7.
  • Subtask 2 (55 points): bi=1b_i = 1.
  • Subtask 3 (1010 points): n90000n \ge 90000, and it is guaranteed that there exists a solution satisfying a1=1,an=na_1 = 1, a_n = n.
  • Subtask 4 (77 points): n90000n \ge 90000, and it is guaranteed that there exists a solution satisfying a2=1,an1=na_2 = 1, a_{n-1} = n.
  • Subtask 5 (1515 points): n100n \le 100, n33×106\sum n^3 \le 3 \times 10^6, with hints for all pathjpath_j.
  • Subtask 6 (1515 points): n100n \le 100, n33×106\sum n^3 \le 3 \times 10^6, with a hint for path0path_0.
  • Subtask 7 (1515 points): n=100n = 100, T=3T = 3, with 55 test points. The input is generated by picking a random aa and then computing f(a)f(a) as the input.
  • Subtask 8 (2525 points): n100n \le 100, n33×106\sum n^3 \le 3 \times 10^6.
  • Subtask 9 (11 point): No special constraints.

Translated by ChatGPT 5