B. Batch Sort
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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:

  1. Swap second and third columns. Now the table is1 2 3 41 4 3 2
  2. In the second row, swap the second and the fourth elements. Now the table is1 2 3 4
  3. 开始没做出来  后来看了别人博客 发现还是不难的 暴力就可以了,先交换两列,然后判断每行不递增的有几个,一旦超过两个的
  4. 交换另一列 当然刚交换的要恢复原样
  5. #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    int a[][];
    int m,n;
    int fun(){
    int i,j;
    for(i=;i<=n;i++){
    int ans=;
    for(j=;j<=m;j++){
    if(a[i][j]!=j){
    ans++;
    }
    }
    if(ans>)
    return ;
    }
    return ;
    }
    int main(){
    int i,j,k;
    scanf("%d%d",&n,&m);
    for(i=;i<=n;i++){
    for(j=;j<=m;j++){
    scanf("%d",&a[i][j]);
    }
    }
    if(m==){
    printf("YES\n");
    return ;
    }
    if(fun()){
    printf("YES\n");
    return ;
    }
    for(i=;i<=m;i++){
    for(j=i+;j<=m;j++){
    for(k=;k<=n;k++){
    swap(a[k][i],a[k][j]);
    }//cout<<fun()<<endl;
    if(fun()==){
    printf("YES\n");
    return ;
    }
    for(k=;k<=n;k++){
    swap(a[k][i],a[k][j]);
    }
    }
    }
    printf("NO\n");
    }

CodeForces 742B Batch Sort的更多相关文章

  1. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

  2. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力

    B. Batch Sort 题目连接: http://codeforces.com/contest/724/problem/B Description output standard output Y ...

  3. CF724B. Batch Sort[枚举]

    B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. Batch Sort

    Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  5. 【39.77%】【codeforces 724B】Batch Sort

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

  6. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)

    传送门 Description You are given a table consisting of n rows and m columns. Numbers in each row form a ...

  7. codeforces 258div2 B Sort the Array

    题目链接:http://codeforces.com/contest/451/problem/B 解题报告:给出一个序列,要你判断这个序列能不能通过将其中某个子序列翻转使其成为升序的序列. 我的做法有 ...

  8. [CF724B]Batch Sort(暴力,思维)

    题目链接:http://codeforces.com/contest/724/problem/B 题意:给出n*m的数字阵,每行数都是1-m的全排列,最多可以交换2个数一次,整个矩阵可以交换两列一次. ...

  9. codeforces 340D Bubble Sort Graph(dp,LIS)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has lea ...

随机推荐

  1. yield个人理解及简明示例

    1.写法有2种:yield return <expression>和yield breakyield用于在迭代中返回一个值,并将值带入下一次迭代中.yield break则意味着停止迭代. ...

  2. 使用php添加定时任务

    1.  php执行外部命令的函数:       system(),exec(),passthru()      注意点:     1.调用的路径,相对路径有时候不是很靠谱.           sys ...

  3. JS中数组Array的用法

    js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了. var arr = new Array();  // 初始化数组arr[0] = "aaa";arr[1] = ...

  4. Spark On Yarn中spark.yarn.jar属性的使用

    今天在测试spark-sql运行在yarn上的过程中,无意间从日志中发现了一个问题: spark-sql --master yarn // :: INFO Client: Requesting a n ...

  5. Sum of Two integers

    两个整数相加不能用加减 用位运算 假设两整数a=2和b=6,它们的二进制表示分别为010和110 sum=a^b表示两个二进制数相加不考虑进位: 010 ^  110 =  100 carry=(a& ...

  6. JS和CSS关于大小写的区分

    方法: document.getElementById("xx").style.xxx中的所有属性是什么 盒子标签和属性对照 CSS语法(不区分大小写) JavaScript语法( ...

  7. [LINUX] 查看连接数和IO负载

    Iostat 是 sysstat 工具集的一个工具,需要安装. Centos的安装方式是: yum install sysstat Ubuntu的安装方式是: aptitude install sys ...

  8. PHP基础课程学习总结

    时间过得很快,不知不觉中过去了一个月,PHP基础课程已经学完了.休息这几天中,睡觉起来,整理下笔记,几天的假期又过去了,明天正式开始PHP的专业课程,新的征途又要开始了.开发整站时发现,过去整站做得太 ...

  9. vc6 编译问题

    Compiling...main.cppLinking...MSVCRT.lib(MSVCRT.dll) : error LNK2005: _malloc already defined in LIB ...

  10. Timus Online Judge 1001. Reverse Root

    Input The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separat ...