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+k−1x,x+1,…,x+k−1 of matrix MM and comes from one of the columns with indeces y,y+1,…,y+k−1y,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 (1≤n,m≤5001≤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 (1≤Aij≤1091≤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 (1≤Bij≤1091≤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\n");
				return 0;
			}
		}
	}
	printf("YES\n");
	return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10518269.html

时间: 2024-11-09 16:00:24

Codeforces Round #546 (Div. 2) C. Nastya Is Transposing Matrices的相关文章

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

题意: 长度为n的数组{pi},m对关系(a,b),如果a正好在数组中位于b的前一个位置,则可以交换a和b,问最多可以让pn的位置往前移动多少 题解: 如果pn可以往前走k步,他肯定可以和pk交换.如果pk后面的数都可与他交换,则最后可以使pn和pk互换,使pn移动到pk的位置 #include <bits/stdc++.h> //#pragma comment(linker, ”/STACK:36777216“) using namespace std; typedef long long

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

题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那么就将\(a_{i+1}\)变成\(a_i+k_i\),如果\(a_{i+2}<a_i+k_i\),则将\(a_{i+2}\)变成\(a_{i+1}+k_{i+1}\),以此类推. 查询\(\sum\limits_{i=l}^{r}a_i\). 思路 我们首先存下\(k\)数组的前缀和\(sum1\),

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

https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则,假如u在v相邻前面,那么u和v可以交换位置,问你是队列最后一个人的时候你最前可以换到前面哪里 题解 因为相邻才能换,所以最后一个换到前面一定是一步一步向前走,所以不存在还要向后走的情况 设最后一个为u,假设前面有一个能和u换位置的集合,那么需要将这些点尽量往后移动去接u 假设前面有一个不能和u换位置的集合S,

Codeforces Round #546 (Div. 2)

http://codeforces.com/contest/1136 A #include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int N, K; struct Node { int L; int R; }node[maxn]; int main() { scanf("%d", &N); for(int i = 1; i <= N; i ++) scanf("

Codeforces Round #489 (Div. 2) E. Nastya and King-Shamans

这道题的算法是: i从1开始,首先求sum(1-i),然后在[i+1, n]中找到第一个a[j]>=sum(1, i) 如果a[j]==sum(1, i)结束搜索,否则令i=j,循环过程 因为每次做完一次之后sum会至少增大一倍,所以一个查询的复杂度会维持到log(Max(a[i])) 需要维护 区间最大值和区间和 的线段树来实现算法 #include <iostream> #include <cstdio> #include <cmath> #include &

Codeforces Round #489 (Div. 2)

Codeforces Round #489 (Div. 2) A. Nastya and an Array #include <bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;++i) #define frep(i,a,b) for(int i=a;i>=b;--i) #define mem(W) memset(W,0,sizeof(W)) #define pb push_back typedef long long ll; c

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我