Visual Python++
Time limit5sMemory limit512 MB
Match n top-left corners to n bottom-right corners so the rectangles form properly nested or disjoint blocks, or report a syntax error.
- Level
Hard8 of 10
- Topics
- Sorting, Stack, Greedy, Implementation
- Solved
- No attempts yet
Problem
In the Visual Python++ programming language, a block of statements is a rectangle of characters. Its top-left corner is in row and column , and its bottom-right corner is in row and column . Every character at a position with and belongs to that block. Among these positions, the ones with , , , or form the border of the block.
Blocks nest to any depth. In a syntactically correct program, two blocks are either nested (one contained in the other) or disjoint (they share no position), and in both cases their borders must not overlap. So when a block sits inside a block , the two satisfy and , and two blocks that are not nested share no position at all.
A programmer does not draw the rectangles. Drawing them takes too long, so a programmer writes one character p at the top-left corner of a block and one character y at its bottom-right corner. The parser then matches the corners and recovers the nesting structure of the program.
Write the part of the parser that performs this matching.
Input
The first line contains an integer (), the number of corner pairs.
Each of the next lines contains two integers and (), a top-left corner in row and column . The next lines give the bottom-right corners in the same format. All corner positions are distinct.
Output
If the corners can be matched so that the blocks form a syntactically correct program, print lines. Line contains the number of the bottom-right corner matched with the -th top-left corner. Top-left corners and bottom-right corners are numbered from 1 to , each group in the order they appear in the input. At most one matching produces a correct program, so the answer is unique.
If no matching produces a correct program, print syntax error on a single line.