#abc277f. F - Sorting a Matrix
F - Sorting a Matrix
Score : points
问题描述
你被给定一个元素为非负整数的矩阵 。对于一对整数 ,满足条件 和 ,令 表示矩阵 中第 行和第 列的元素。
让我们对矩阵 执行以下操作:
-
首先,将矩阵 中所有值为 的元素用任意一个 正整数 替换(若存在多个 元素,可以替换为不同的正整数)。
-
然后,重复执行以下两种操作之一,每次可以选择,且可以执行任意多次(可能为零次):
-
选择一对整数 满足条件 ,交换矩阵 中第 行和第 行。
-
选择一对整数 满足条件 ,交换矩阵 中第 列和第 列。
-
判断矩阵 是否能够通过上述操作满足以下条件:
-
$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}$。
-
换句话说,对于任意两对整数 和 ,满足条件 以及 ,同时满足以下两个条件:
-
若 ,则有 。
-
若 且 ,则有 。
-
以上为通义千问 qwen-max 翻译,仅供参考。
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 .
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 .
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
.
Sample Input 1
3 3
9 6 0
0 4 0
3 0 3
Sample Output 1
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
Sample Input 2
2 2
2 1
1 2
Sample Output 2
No
There is no way to perform the operations to make satisfy the condition in the problem statement, so you should print No
.
update @ 2024/3/10 11:40:40