链接:
https://ac.nowcoder.com/acm/contest/57362/K
来源:牛客网
The Animals Corporation has assigned a new task with codename S to its staff!
The fox and the cat have received an unsorted matrix
AAA with
nnn rows and
mmm columns. The elements of the matrix (
Ai,jA_{i,j}Ai,j) are integers from
111 to
n×mn \times mn×m and are pairwise distinct. Their mission is to make the matrix sorted. Here we call matrix
AAA sorted, if and only if its elements are non-decreasing in reading order (i.e.,
A1,1≤A1,2≤⋯≤A1,m≤A2,1≤A2,2≤⋯A2,m≤⋯≤An,1≤An,2≤⋯≤An,mA_{1,1} \le A_{1,2} \le \cdots \le A_{1,m} \le A_{2,1} \le A_{2,2} \le \cdots A_{2,m} \le \cdots \le A_{n,1} \le A_{n,2} \le \cdots \le A_{n,m}A1,1≤A1,2≤⋯≤A1,m≤A2,1≤A2,2≤⋯A2,m≤⋯≤An,1≤An,2≤⋯≤An,m).
To finish the task, the furry groupmates have a clear division of labor. Specifically, the fox can only perform the following operation:
-
Choose integers i,ji,ji,j (1≤i<j≤n1 \le i < j \le n1≤i<j≤n), and swap the iii-th row and the jjj-th row of matrix AAA.
And the cat can only perform the following operation:
-
Choose integers i,ji,ji,j (1≤i<j≤m1 \le i < j \le m1≤i<j≤m), and swap the iii-th column and the jjj-th column of matrix AAA.
The groupmates operate in turn (The fox goes first, the cat next, then the fox again, and so on). In their turn, one is
not allowed to perform multiple operations or give up performing the operation.
It is a golden opportunity to develop team spirit. However, both the fox and the cat desire to win their boss's favor by becoming the first one who makes the matrix sorted after his own operation. And what's worse is that, if one knows he has no chance of meeting such desire, he will try to prevent his groupmate from being the first to sort the matrix, even if the matrix will remain unsorted forever!
You, a mysterious staff member, have seen through their charade. Assuming both the fox and the cat are clever enough, you wonder who will be the first to make the matrix sorted.