Sort the Table
InterviewTime limit2sMemory limit512 MB
Given an N by M grid holding a permutation of 1 to N*M, decide whether swapping whole rows and whole columns can make the row-major reading order sorted ascending.
- Level
Medium6 of 10
- Topics
- Sorting, Implementation, Matrix, Greedy
- Solved
- No attempts yet
Problem
Yeongseon has a table with rows and columns. The rows are numbered to from top to bottom, and the columns are numbered to from left to right.
Each cell holds one positive integer, and the numbers in the table form a permutation of to . Every number appears exactly once.
Read the cells row by row starting from the first row, and inside a row from left to right. The sequence you get is called the value sequence.
Yeongseon can modify the table in two ways. She can swap the positions of any two rows, or swap the positions of any two columns. She can use each operation as many times as she wants, in any order.
Given the table, write a program that decides whether the value sequence can be made ascending.
Input
The first line contains and . ()
Each of the next lines contains numbers, the contents of the table.
Output
Print if the value sequence can be made ascending, and otherwise.