Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices

Posted echozqn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices相关的知识,希望对你有一定的参考价值。

C. Nastya Is Transposing Matrices
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task.

Two matrices AA and BB are given, each of them has size n×mn×m . Nastya can perform the following operation to matrix AA unlimited number of times:

  • take any square square submatrix of AA and transpose it (i.e. the element of the submatrix which was in the ii -th row and jj -th column of the submatrix will be in the jj -th row and ii -th column after transposing, and the transposed submatrix itself will keep its place in the matrix AA ).

Nastya‘s task is to check whether it is possible to transform the matrix AA to the matrix BB .

技术图片 Example of the operation

As it may require a lot of operations, you are asked to answer this question for Nastya.

A square submatrix of matrix MM is a matrix which consist of all elements which comes from one of the rows with indeces x,x+1,,x+k1x,x+1,…,x+k−1 of matrix MM and comes from one of the columns with indeces y,y+1,,y+k1y,y+1,…,y+k−1 of matrix MM . kk is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes).

Input

The first line contains two integers nn and mm separated by space (1n,m5001≤n,m≤500 ) — the numbers of rows and columns in AA and BB respectively.

Each of the next nn lines contains mm integers, the jj -th number in the ii -th of these lines denotes the jj -th element of the ii -th row of the matrix AA (1Aij1091≤Aij≤109 ).

Each of the next nn lines contains mm integers, the jj -th number in the ii -th of these lines denotes the jj -th element of the ii -th row of the matrix BB (1Bij1091≤Bij≤109 ).

Output

Print "YES" (without quotes) if it is possible to transform AA to BB and "NO" (without quotes) otherwise.

You can print each letter in any case (upper or lower).

Examples
Input
Copy
2 2
1 1
6 1
1 6
1 1
Output
Copy
YES
Input
Copy
2 2
4 4
4 5
5 4
4 4
Output
Copy
NO
Input
Copy
3 3
1 2 3
4 5 6
7 8 9
1 4 7
2 5 6
3 8 9
Output
Copy
YES
 
 
这个题目属于思维题,需要你细心观察,首先明确一下,用搜索是不可取的,反正我是想不到怎么去搜索,其次就是这个题目你好好观察给你的条件,
一个是可以交换无数次,然后又是只能对角交换,仔细想想应该可以知道,对角的元素都是可以互换的,所以你把所有的对角元素枚举,然后你再进行比较,
只要有元素在另一个矩阵这个对角中找不到,那就说明不能换。
 
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 550;
int a[maxn][maxn];
int b[maxn][maxn];
int ta[maxn*maxn];
int tb[maxn*maxn];

int main()
{
	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
			scanf("%d", &a[i][j]);
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			scanf("%d", &b[i][j]);
		}
	}
	for (int i = 1; i <= n + m; i++)
	{
		int cnta = 0, cntb = 0;
		for (int j = 1; j < i; j++)
		{
			if (j <= n && i - j <= m && i - j >= 1)
			{
				ta[++cnta] = a[j][i - j];
			}
		}
		for (int j = 1; j < i; j++)
		{
			if (j <= n && i - j <= m && i - j >= 1)
			{
				tb[++cntb] = b[j][i - j];
			}
		}
		sort(ta + 1, ta + cnta + 1);
		sort(tb + 1, tb + 1 + cntb);
		for (int i = 1; i <= cnta; i++)
		{
			if (ta[i] != tb[i])
			{
				printf("NO
");
				return 0;
			}
		}
	}
	printf("YES
");
	return 0;
}

  

 

以上是关于Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #546 (Div. 2) D 贪心 + 思维

Codeforces Round #546 (Div. 2)-D - Nastya Is Buying Lunch

Codeforces Round #546 (Div. 2) D. Nastya Is Buying Lunch

Codeforces-Round#546(Div.2)-D-Nastya Is Buying Lunch

Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)

Codeforces Round #436 E. Fire(背包dp+输出路径)