You are given two ?×?n×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 [91110121114][91011111214] is increasing because each individual row and column is strictly increasing. On the other hand, the matrix [1213][1123] is not increasing because the first row is not strictly increasing.
Let a position in the ?i-th row (from top) and ?j-th column (from left) in a matrix be denoted as (?,?)(i,j).
In one operation, you can choose any two numbers ?i and ?j and swap the number located in (?,?)(i,j) in the first matrix with the number in (?,?)(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".
Input
The first line contains two integers ?n and ?m (1≤?,?≤501≤n,m≤50) — the dimensions of each matrix.
Each of the next ?n lines contains ?m integers ??1,??2,…,???ai1,ai2,…,aim (1≤???≤1091≤aij≤109) — the number located in position (?,?)(i,j) in the first matrix.
Each of the next ?n lines contains ?m integers ??1,??2,…,???bi1,bi2,…,bim (1≤???≤1091≤bij≤109) — the number located in position (?,?)(i,j) in the second matrix.
Output
Print a string "Impossible" or "Possible".
codeforces1162B
Examples
input
2 2
2 10
11 5
9 4
3 12
output
Possible
input
2 3
2 4 5
4 5 6
3 6 7
8 10 11
output
Possible
input
3 2
1 3
2 4
5 10
3 1
3 6
4 8
output
Impossible
题意:给出两个n*m的数组,问通过对应位置互换可以使这两个数组变为行列都从左上角开始严格递增的吗?
思路:可以先将两个数组里面对应位置小的放到第一个数组里面,大的放到第二个数组里面,然后看两个数组是否满足就可以了。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define MAX 0x3f3f3f3f
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const double PI = acos(-1);
const int M=2e5+10;
int main()
{
int a[100][100],b[100][100],i,j,flag=0,n,m;
memset(a,0x3f3f3f,sizeof(a));//不能赋0,因为这样后面会交换位置
memset(b,0x3f3f3f,sizeof(b));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&b[i][j]);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(a[i][j]>b[i][j])
swap(a[i][j],b[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(a[i][j]>=a[i][j+1]||a[i][j]>=a[i+1][j])
flag=1;
if(b[i][j]>=b[i][j+1]||b[i][j]>=b[i+1][j])
flag=1;
}
}
if(flag==0)
printf("Possible\n");
else
printf("Impossible\n");
return 0;
}