#P3832. [SHOI2012] 排序
[SHOI2012] 排序
Description
As is well known, the set of all permutations of contains permutations. Normally, when we generate all permutations, we list them in lexicographic order. In this problem, we consider a special way of generating all permutations.
Specifically, the generation order is determined by a generator.
(1) The generator is itself a permutation of : , , …, .
(2) For two distinct permutations of , , , …, and , , …, , first find the smallest such that and are not equal.
(3) According to the chosen in (2), if comes before in the permutation , , …, , then , , …, will be generated before , , …, .
For example, when and the generator is 132, the generation order of all permutations of is: 123, 132, 321, 312, 231, 213.
Given a permutation , , …, , determine which generator makes this permutation appear as early as possible among all permutations, and which generator makes it appear as late as possible.
If multiple generators satisfy the requirement, output the lexicographically smallest one.
Input Format
The first line contains an integer . The second line contains a permutation , , …, of .
Output Format
The first line outputs a permutation of , representing a generator that makes , , …, appear as early as possible.
The second line outputs a permutation of , representing a generator that makes , , …, appear as late as possible.
If multiple generators satisfy the requirement, output the lexicographically smallest one.
3
1 3 2
1 2 3
2 1 3
Hint
Constraints
- For 30% of the testdata, .
- For 50% of the testdata, .
- For 90% of the testdata, .
- For 100% of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号