#P5754. [JSOI2010] 排名

[JSOI2010] 排名

Background

On Arbor Day, Little L, Little H, and Little X still had to face a busy exam.

After the exam, as usual, they discussed their scores. Little L was very curious, so he asked all NN students in the class about their scores. Of course, just as you might expect, nobody wanted to reveal too much: each person only said whose score they were lower than, and some people did not say anything.

Hardworking Little H and lazy Little X each have an “expected ranking” in mind for all students in the class. That is, Little H may think XX should be ranked 11st, YY should be ranked 22nd, and so on. But Little X would think XX should be ranked last (the 11st from the end), YY should be ranked the 22nd from the end, and so on.

However, ideals and reality always differ. From the information that Little L gathered, XX can no longer be ranked 11st. Still, Little H believes XX should be ranked as high as possible.

So Little L came up with a question: after Little H and Little X learn the information he gathered, what will their “mental rankings” of the class look like?

Each student’s index is exactly Little H’s mental preference order. In other words, Little H hopes that students with smaller indices will be ranked as high as possible, while Little X hopes that students with smaller indices will be ranked as low as possible (note: this does not mean that students with larger indices should be ranked higher).

Problem Description

Given an array AA of length NN, where AiA_i means that the ii-th student’s score is lower than the AiA_i-th student’s score (equivalently, the ii-th student is ranked after the AiA_i-th student). Of course, AiA_i may equal 00, which means there is no information about the ii-th student.

You need to obtain an array HH of length NN, representing the class ranking. Among all rankings that satisfy all constraints given by AiA_i, this ranking should be the lexicographically smallest one.

At the same time, you also need to obtain an array XX, representing the class ranking. Among all rankings that satisfy all constraints given by AiA_i, this ranking should be the lexicographically largest one.

Input Format

The first line contains a positive integer NN, the number of students in the class.

The second line contains NN non-negative integers separated by spaces. The ii-th number denotes AiA_i.

Output Format

Output two lines. Each line contains NN positive integers separated by spaces. The first line is Little H’s mental ranking, and the second line is Little X’s mental ranking.

4
3 0 2 2
3 1 2 4
4 1 3 2

Hint

Sample Explanation

There are 33 rankings that satisfy the ordering constraints:

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

Among them, 3 1 2 4 is lexicographically smallest, and 4 1 3 2 is lexicographically largest.

Constraints

For 10%10\% of the testdata, N10N\leq 10.

For 20%20\% of the testdata, N20N\leq 20.

For 40%40\% of the testdata, N2×103N\leq 2\times 10^3.

For 100%100\% of the testdata, 1N2×105,AiN1 \leq N\leq 2\times 10^5, A_i\leq N. Among them, the 55-th testdata group guarantees N=1.2×104N=1.2\times 10^4.

Translated by ChatGPT 5