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. U盘安装ubuntu server 12.04的问题检测不到CDROM的解决

    U盘安装ubuntu server 12.04的问题检测不到CDROM的解决 ========================== 我是u盘安装ubuntu 14 64Bit 也是出现同样的问题 用u ...

  2. Laxcus大数据管理系统单机集群版

    Laxcus大数据管理系统是我们Laxcus大数据实验室历时5年,全体系全功能设计研发的大数据产品,目前的最新版本是2.1版本.从三年前的1.0版本开始,Laxcus大数据系统投入到多个大数据和云计算 ...

  3. WinCE系统深度定制汇总

    WinCE应用程序崩溃提示框的处理 WinCE6.0多国语言软键盘 定制WinCE6.0标准界面的一种方法 如何禁用WinCE下的沙漏图标 Silent RNAApp.exe for WinCE6.0 ...

  4. js 分享到按钮

    基础的思路,可以在此基础加强 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  5. 初学matlab----函数用法(随学习更新中)

    sort(A) 若A是向量不管是列还是行向量,默认都是对A进行升序排列. sort(A)是默认的升序,而sort(A,'descend')是降序排序. sort(A)若A是矩阵,默认对A的各列进行升序 ...

  6. singleTask, singleInstance使用心得

    1. singleTask, singleInstance 共同点: 1)  一旦入栈,都为于栈底. 2) 全栈有且只有一个activity实例对象. 2. singleTask, singleIns ...

  7. random、面向对象编程

    一.random模块:随机数 import random print random.random() print random.randint(,) print random.randrange(,) ...

  8. 关于Log和adb知识

    1,打印日志:adb logcat -v time >log 2,清除以上日志:adb logcat -c 2,查看设备是否连接电脑:adb devices 3,登陆手机设备:adb shell ...

  9. VS&SQL StartUp Crash - CLR20R3

    VS2013和SQL Management Studio 在启动时直接崩溃,错误提示CLR20R3,问题签名4是windowsbase, 这说明是操作系统的问题导致启动崩溃,在网上找到一些解决方案: ...

  10. Spring 4集成 Quartz2(转)

    本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度.Spring 为简化 Quartz 的操作提供了相关支持类.本文示例使用的相关工具如下: Spring 4.2. ...