Rooks
Time limit1sMemory limit128 MB
Place n non-attacking rooks, one per given axis-aligned rectangle, or report that no placement exists; output the lexicographically smallest placement.
- Level
Hard8 of 10
- Topics
- Greedy, Sorting, Binary search, Implementation
- Solved
- No attempts yet
Problem
On an chessboard () we place rooks, one per rook index. The placement must satisfy the following rules.
- For each , rook must be placed inside a rectangle given by two corners and , where is the top-left square (row, column) and is the bottom-right square, with and . The top-left square of the board is and the bottom-right square is . In other words, the row of rook must lie in and its column must lie in .
- No two rooks may attack each other, meaning no two rooks share the same row or the same column.
Decide whether all rooks can be placed inside their rectangles without any two attacking each other, and if so, output one such placement.
Input
The first line contains a single integer (). Each of the next lines contains four integers , , , separated by single spaces, describing the rectangle for rook (, , every value between and ).
Output
If no valid placement exists, print the single word NIE (Polish for "no").
Otherwise print lines. Line contains the row and column of rook separated by a single space, where the row lies in and the column lies in ; the rooks are listed in the same order as their rectangles were given.
Because several placements can be valid, print the lexicographically smallest one. Compare two placements by the sequence formed by reading the values in order: row of rook , column of rook , row of rook , column of rook , and so on. Equivalently, make the row of rook as small as possible, then its column as small as possible, then the row of rook , then its column, and so on.