#P9467. [EGOI 2023] Carnival General / 狂欢节总管
[EGOI 2023] Carnival General / 狂欢节总管
Background
Day 2 Problem A.
Translated from EGOI2023 carnival。
Problem Description
Every four years, students in Lund gather and host the Lund Carnival. Over a few days, the park fills up with tents and many kinds of celebrations are held. The person responsible for organizing all of this is called the Carnival General.
There are carnivals, each with a different general. These generals are numbered from to in chronological order. Each general gives their opinion on previous generals: they publish a ranking of generals from best to worst.
The next Lund Carnival will be held in 2026. During that time, all former carnival generals got together to take a group photo. However, it will be awkward if generals and (where ) end up standing next to each other, and is ranked strictly in the bottom half of the ranking given by .
For example:
- If the ranking given by general is
3 2 1 0, then may stand next to or , but may not stand next to or . - If the ranking given by general is
4 3 2 1 0, then may stand next to , but may not stand next to or .
The figure below shows Sample . In it, general stands next to generals and , and general stands only next to general .

You know all the rankings given by the generals. Your task is to arrange all generals in a line such that whenever and stand next to each other (where ), then is not ranked strictly in the bottom half of the ranking given by .
Input Format
The first line contains an integer , the total number of generals.
The next lines contain all rankings. The first of these lines is the ranking given by general , the second is the ranking given by general , and so on up to general . General is not included in the input, because general has no previous generals to rank.
The ranking of general is a list of length : , where each integer from to appears exactly once. Specifically, in the ranking of general , is the best general and is the worst general.
Output Format
Output a sequence of integers: a permutation of , such that for every pair of adjacent integers, neither one is in the bottom half of the other one's ranking.
It can be proven that a solution always exists. If there are multiple solutions, you may output any of them.
6
0
1 0
2 1 0
3 2 1 0
4 3 2 1 0
4 2 5 3 1 0
5
0
0 1
0 1 2
0 1 2 3
2 0 4 1 3
4
0
1 0
0 2 1
3 0 1 2
Hint
Sample Explanation
Sample satisfies the requirements of Subtask 1. In the sample, generals and cannot stand next to general , and generals and cannot stand next to generals and . The sample output is shown in the figure above.
Sample Explanation
Sample satisfies the requirements of Subtask 2. In the sample, general cannot stand next to general , general cannot stand next to general , and general cannot stand next to generals and .
Sample Explanation
Sample satisfies the requirements of Subtask 3. In the sample, only the two pairs and of generals cannot stand next to each other. Therefore, if the arrangement is 3 0 1 2, there will be no conflict. Another possible answer is 0 1 2 3.
Constraints
For all testdata, , 。
- Subtask 1 ( points): the ranking given by general is .
- Subtask 2 ( points): the ranking given by general is .
- Subtask 3 ( points): .
- Subtask 4 ( points): no additional constraints.
Translated by ChatGPT 5
