atcoder#ABC277F. [ABC277F] Sorting a Matrix
[ABC277F] Sorting a Matrix
Score : points
Problem Statement
You are given a matrix whose elements are non-negative integers. For a pair of integers such that and , let denote the element at the -th row and -th column of .
Let us perform the following procedure on .
- First, replace each element of that is with an arbitrary positive integer (if multiple elements are , they may be replaced with different positive integers).
- Then, repeat performing one of the two operations below, which may be chosen each time, as many times as desired (possibly zero).
- Choose a pair of integers such that and swap the -th and -th rows of .
- Choose a pair of integers such that and swap the -th and -th columns of .
- Choose a pair of integers such that and swap the -th and -th rows of .
- Choose a pair of integers such that and swap the -th and -th columns of .
Determine whether can be made to satisfy the following condition.
- $A_{1, 1} \leq A_{1, 2} \leq \cdots \leq A_{1, W} \leq A_{2, 1} \leq A_{2, 2} \leq \cdots \leq A_{2, W} \leq A_{3, 1} \leq \cdots \leq A_{H, 1} \leq A_{H, 2} \leq \cdots \leq A_{H, W}$.
- In other words, for every two pairs of integers and such that and , both of the following conditions are satisfied.
- If , then .
- If and , then .
- If , then .
- If and , then .
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
If can be made to satisfy the condition in the problem statement, print Yes
; otherwise, print No
.
3 3
9 6 0
0 4 0
3 0 3
Yes
One can perform the operations as follows to make satisfy the condition in the problem statement, so you should print Yes
.
- First, replace the elements of that are , as shown below:
9 6 8
5 4 4
3 1 3
- Swap the second and third columns. Then, becomes:
9 8 6
5 4 4
3 3 1
- Swap the first and third rows. Then, becomes:
3 3 1
5 4 4
9 8 6
- Swap the first and third columns. Then, becomes the following and satisfies the condition in the problem statement.
1 3 3
4 4 5
6 8 9
2 2
2 1
1 2
No
There is no way to perform the operations to make satisfy the condition in the problem statement, so you should print No
.