Sorting
InterviewTime limit1sMemory limit1024 MB
A full comparison table between two hidden permutations of 1..N is given; recover the value on every card.
- Level
Easy3 of 10
- Topics
- Sorting, Implementation
- Solved
- No attempts yet
Problem
Jonas and Artūras are playing a game. Jonas lays out two rows of cards, each row containing cards. Every card has an integer between and written on it, and within a single row each value from to appears exactly once — so each row is a permutation of .
Artūras cannot see the numbers. To learn them, he compares cards across the two rows: he picks one card from the first row and one card from the second row and asks Jonas which of the two values is larger. Jonas replies that the first card is larger, that the second card is larger, or that the two cards are equal.
Artūras has already compared every card of the first row against every card of the second row and recorded all of the answers as a table. Let the result of comparing the -th card of the first row with the -th card of the second row be the integer :
- if the first row's card is larger,
- if the second row's card is larger,
- if the two cards are equal.
Given the whole table, determine the integer written on every card in both rows.
Input
The first line contains one integer — the number of cards in each row.
Each of the next lines contains integers. The -th integer on the -th of these lines is , the comparison result between the -th card of the first row and the -th card of the second row.
Output
Print two lines.
- The first line contains the values written on the cards of the first row, in order, separated by single spaces.
- The second line contains the values of the second row, in the same format.
The answer is uniquely determined by the input.
Constraints
- Every comparison result satisfies .
- The table is always consistent with two permutations of , so a valid answer exists and is unique.