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;
}

  

 

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

  1. Codeforces Round #546 (Div. 2) B. Nastya Is Playing Computer Games

    链接:https://codeforces.com/contest/1136/problem/B 题意: 有n个井盖,每个井盖上有一个小石头. 给出n和k,k表示刚开始在第k个井盖上方. 有三种操作, ...

  2. Codeforces Round #546 (Div. 2) A. Nastya Is Reading a Book

    链接:https://codeforces.com/contest/1136/problem/A 题意: 给n个区间,每个区间范围不超过100,n不超过100. 给一个位置k,1-(k-1)是遍历过的 ...

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

    这道题,神仙贪心题... 题意就是我给出数的顺序,并给出多个交换,每个只能用于相邻交换,问最后一个元素,最多能往前交换多少步. 我们考虑这样一个问题,如果一个这数和a[n]发生交换,那么这个数作为后面 ...

  4. Codeforces Round #546 (Div. 2) E - Nastya Hasn't Written a Legend

    这题是一个贼搞人的线段树 线段树维护的是 区间和a[i - j] 首先对于update的位置可以二分查找 其次update时候的lazy比较技巧 比如更新的是 l-r段,增加的是c 那么这段的值为: ...

  5. Codeforces Round #546 (Div. 2) 题解

    Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...

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

    题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那 ...

  7. Codeforces Round #546 (Div. 2)

    http://codeforces.com/contest/1136 A #include <bits/stdc++.h> using namespace std; ; int N, K; ...

  8. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

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

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

随机推荐

  1. SQL Server 怎么在分页获取数据的同时获取到总记录数

    SQL Server 获取数据的总记录数,有两种方式: 1.先分页获取数据,然后再查询一遍数据库获取到总数量 2.使用count(1) over()获取总记录数量 SELECT * FROM ( SE ...

  2. 1.shell学习之常用语句

    本章学习内容: 1.常见符号 2.命令分隔符-分号";" 3.双分号 ";;" 4. '单引号和"双引号 5.`反引号和$() 6.${}和$ 7.冒 ...

  3. 【Java每日一题】20170321

    20170320问题解析请点击今日问题下方的“[Java每日一题]20170321”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...

  4. .NET 发送电子邮件

    static void Main(string[] args) { ///先引入 using System.Net.Mail; ///发送邮件 using (MailMessage mailMessa ...

  5. CommandLineRunner和ApplicationRunner的区别

    CommandLineRunner和ApplicationRunner的区别 二者的功能和官方文档一模一样,都是在Spring容器初始化完毕之后执行起run方法 不同点在于,前者的run方法参数是St ...

  6. C#导入Excel、Excel导入、导入.xls 、导入.xlsx、Excel2003版本、Excel2007版本

    C#导入Excel: 1.选择Excel 03版文件 2.选择需要读取数据的Excel工作表   3.选择工作表中需要读取的列 源码地址在图片下面,不要点击图片,点击下载地址跳转下载.

  7. 【资料】Mac OS X 10.9虚拟机镜像及tools

    原文链接 http://bbs.itheima.com/thread-141793-1-1.html 1.首先解压文件,两个同时选中,右键进行解压. 2.打开虚拟机VMware workstation ...

  8. Android联系人列表实现

    演示 汉字转拼音 String pinyingStr = PinyinHelper.getShortPinyin( String.valueOf(charSequence).toLowerCase() ...

  9. 自己动手写Android插件化框架,让老板对你刮目相看

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由达文西发表于云+社区专栏 最近在工作中接触到了Android插件内的开发,发现自己这种技术还缺乏最基本的了解,以至于在一些基本问题上浪 ...

  10. SELinux app权限配置

    摘要:1.SEAndroidapp分类SELinux(或SEAndroid)将app划分为主要三种类型(根据user不同,也有其他的domain类型):1)untrusted_app 第三方app,没 ...