time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

You are given a table consisting of n rows and m columns.

Numbers in each row form a permutation of integers from 1 to m.

You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order.

You have to check whether it’s possible to obtain the identity permutation 1, 2, …, m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table.

Each of next n lines contains m integers — elements of the table. It’s guaranteed that numbers in each line form a permutation of integers from 1 to m.

Output

If there is a way to obtain the identity permutation in each row by following the given rules, print “YES” (without quotes) in the only line of the output. Otherwise, print “NO” (without quotes).

Examples

input

2 4

1 3 2 4

1 3 4 2

output

YES

input

4 4

1 2 3 4

2 3 4 1

3 4 1 2

4 1 2 3

output

NO

input

3 6

2 1 3 4 5 6

1 2 4 3 5 6

1 2 3 4 6 5

output

YES

Note

In the first sample, one can act in the following way:

Swap second and third columns. Now the table is

1 2 3 4

1 4 3 2

In the second row, swap the second and the fourth elements. Now the table is

1 2 3 4

1 2 3 4

【题解】



先枚举那个交换整列的操作。

然后用这道题的做法判断每一行是不是可以在每一行交换一次元素过后变成有序:

http://blog.csdn.net/harlow_cheng/article/details/52294871

就是找循环节吧。交换

#include <cstdio>
#include <algorithm>
#include <iostream> using namespace std; const int MAXN = 25; struct abc
{
int data;
int key;
}; int n, m;
int a[MAXN][MAXN];
abc b[MAXN];
bool vis[MAXN]; void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 +t- '0', t = getchar();
} bool cmp(abc a, abc b)
{
return a.data < b.data;
} int get_num(int x)
{
for (int i = 1; i <= m; i++)
b[i].data = a[x][i], b[i].key = i;
sort(b + 1, b + 1 + m, cmp);
for (int i = 1; i <= m; i++)
vis[i] = false;
int num = 0;
for (int i = 1; i <= m; i++)
if (!vis[i])
{
int t = i;
int len = 0;
while (!vis[t])
{
vis[t] = true;
len++;
t = b[t].key;
}
num += len - 1;//len-1就是需要交换的次数
}
return num;
} bool check()
{
for (int i = 1; i <= n; i++)
{
int num = get_num(i);
if (num > 1)
return false;
}
return true;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n); input(m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
input(a[i][j]);
bool flag = false;
flag = check();
if (flag)
{
puts("YES");
return 0;
}
for (int i = 1;i <= m-1;i++)
for (int j = i + 1; j <= m; j++)
{
for (int k = 1; k <= n; k++)
swap(a[k][i], a[k][j]);
flag = check();
if (flag)
{
puts("YES");
return 0;
}
for (int k = 1; k <= n; k++)
swap(a[k][i], a[k][j]);
}
puts("NO");
return 0;
}

【39.77%】【codeforces 724B】Batch Sort的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【77.39%】【codeforces 734A】Anton and Danik

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【39.66%】【codeforces 740C】Alyona and mex

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【13.77%】【codeforces 734C】Anton and Making Potions

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 750D】New Year and Fireworks

    time limit per test2.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...

随机推荐

  1. JS错误记录 - 微博发布

    <style> *{ margin: 0; padding: 0;} #ul1{ width: 400px; height: 400px; border: 1px solid #000; ...

  2. IR_drop

    IR压降是指出现在集成电路中电源和地网络上电压下降或升高的一种现象.随着半导体工艺的演进金属互连线的宽度越来越窄,导致它的电阻值上升,所以在整个芯片范围内将存在一定的IR压降.IR压降的大小决定于从电 ...

  3. Hadoop读书笔记(一)Hadoop介绍

    1.Hadoop是什么? 适合大数据的分布式与计算平台. HDFS:Hadoop Distributed File System 分布式文件系统  MapReduce:并行计算框架 2.HDFS的架构 ...

  4. 【BZOJ 4516】生成魔咒

    [链接]h在这里写链接 [题意]     [Description]         给你n(n<=10^9)个数字,把它们依次,一个一个地添加在空串S的后面.         要求每添加一次之 ...

  5. 更改jdk所用内存空间

    在做项目是有时候会遇到内存jvm内存不够用的情况,在myeclipse是这样设置的. -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=128m

  6. gdbserver远程调试嵌入式linux应用程序方法

    此处所讲的是基于gdb和gdbsever的远程调试方法.环境为:PC机:win7.虚拟机:10.04.下位机:飞嵌TE2440开发板. 嵌入式linux应用程序的开发一般都是在linux里面编写好代码 ...

  7. -bash: /usr/local/mysql/scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory

    安装 MySQL 初始化时,报错如下: [root@hcdb1 ~]# /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/m ...

  8. GCD 初步学习

    GCD应该是比較牛逼的东西了吧,一时半会应该是操作不好. 在cocoa-china上面有两篇关于GCD的文章.GCD 深入理解(一) GCD 深入理解(二) CSDN荣芳志博客:点击打开链接 我仅仅是 ...

  9. [CSS] Control Image Aspect Ratio Using CSS

    Resize images and videos to fill their parent and maintain their aspect ratio with pure CSS. The new ...

  10. Nginx+ 多个Memcached+ 多个Tomcat集群配置来实现 sticky Session

    假如有 大于2 台的Tomcat servers,如何实现sticky session特点的高可靠web 服务? 方案设计: 前端使用nginx(最好是淘宝的 tengine)作为we 流量分发器,向 ...