CF1162B.Double Matrix

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given two n×mn \times m matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing.

For example, the matrix [91011111214]\begin{bmatrix} 9&10&11\\ 11&12&14\\ \end{bmatrix} is increasing because each individual row and column is strictly increasing. On the other hand, the matrix [1123]\begin{bmatrix} 1&1\\ 2&3\\ \end{bmatrix} is not increasing because the first row is not strictly increasing.

Let a position in the ii-th row (from top) and jj-th column (from left) in a matrix be denoted as (i,j)(i, j).

In one operation, you can choose any two numbers ii and jj and swap the number located in (i,j)(i, j) in the first matrix with the number in (i,j)(i, j) in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.

You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible".

给你两个 n×mn \times m 的整数矩阵。一个整数序列被称为严格递增的,当且仅当每个后续数字都严格大于前一个数字。一个行是严格递增的,当且仅当该行中从左到右的所有数字严格递增;一个列是严格递增的,当且仅当该列中从上到下的所有数字严格递增。一个矩阵是递增的,当且仅当它的所有行和所有列都是严格递增的。

例如,矩阵 [91011111214]\begin{bmatrix} 9&10&11\\ 11&12&14\\ \end{bmatrix} 是递增的,因为它的每一行和每一列均严格递增。而矩阵 [1123]\begin{bmatrix} 1&1\\ 2&3\\ \end{bmatrix} 不是递增的,因为第一行不是严格递增的。

记矩阵中第 ii 行(从上往下数)第 jj 列(从左往右数)的位置为 (i,j)(i, j)。

一次操作定义为:任选一对下标 ii 和 jj,将第一个矩阵中位置 (i,j)(i, j) 处的数与第二个矩阵中位置 (i,j)(i, j) 处的数进行交换。换言之,你只能交换两个矩阵中相同位置上的两个数。

你希望经过若干次(可能为零次)上述操作后,使得两个矩阵均为递增矩阵。请判断是否可行。若可行,输出 "Possible";否则,输出 "Impossible"。

输入格式

The first line contains two integers nn and mm (1≤n,m≤501 \leq n,m \leq 50) — the dimensions of each matrix.

Each of the next nn lines contains mm integers ai1,ai2,…,aima_{i1}, a_{i2}, \ldots, a_{im} (1≤aij≤1091 \leq a_{ij} \leq 10^9) — the number located in position (i,j)(i, j) in the first matrix.

Each of the next nn lines contains mm integers bi1,bi2,…,bimb_{i1}, b_{i2}, \ldots, b_{im} (1≤bij≤1091 \leq b_{ij} \leq 10^9) — the number located in position (i,j)(i, j) in the second matrix.

第一行包含两个整数 nn 和 mm(1≤n,m≤501 \leq n,m \leq 50)—— 分别表示每个矩阵的维度。

接下来的 nn 行,每行包含 mm 个整数 ai1,ai2,…,aima_{i1}, a_{i2}, \ldots, a_{im}(1≤aij≤1091 \leq a_{ij} \leq 10^9)—— 表示第一个矩阵中位置 (i,j)(i, j) 处的数值。

再接下来的 nn 行,每行包含 mm 个整数 bi1,bi2,…,bimb_{i1}, b_{i2}, \ldots, b_{im}(1≤bij≤1091 \leq b_{ij} \leq 10^9)—— 表示第二个矩阵中位置 (i,j)(i, j) 处的数值。

输出格式

Print a string "Impossible" or "Possible".

打印字符串“Impossible”或“Possible”。

输入输出样例

  • 输入#1

    2 2
    2 10
    11 5
    9 4
    3 12

    输出#1

    Possible
  • 输入#2

    2 3
    2 4 5
    4 5 6
    3 6 7
    8 10 11

    输出#2

    Possible
  • 输入#3

    3 2
    1 3
    2 4
    5 10
    3 1
    3 6
    4 8

    输出#3

    Impossible

说明/提示

The first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be [9101112]\begin{bmatrix} 9&10\\ 11&12\\ \end{bmatrix} and [2435]\begin{bmatrix} 2&4\\ 3&5\\ \end{bmatrix}.

In the second example, we don't need to do any operations.

In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.

第一个例子中,我们可以对两个矩阵的左上角和右下角单元格执行一次操作。操作后得到的矩阵分别为 [9101112]\begin{bmatrix} 9&10\\ 11&12\\ \end{bmatrix} 和 [2435]\begin{bmatrix} 2&4\\ 3&5\\ \end{bmatrix}。

第二个例子中,我们无需执行任何操作。

第三个例子中,无论怎样交换,我们都无法使两个矩阵的第一行同时严格递增。

输入解题思路,AI测评打分。不知道怎么写?

首页