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. 【转】C# 使用消息队列,包括远程访问

    出处:http://www.cnblogs.com/80X86/p/5557801.html 近期做一个小的功能需求,用到了队列,用的时候出了很多问题,现在总结一下,希望能对有需要的人提供帮助. 我的 ...

  2. python sorted和sort

    我们需要对List进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序 方法2.用built-in函数sorted进行排序(从2.4开始) ...

  3. (Array)27. Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  4. JAVA设计模式之不变模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述不变(Immutable)模式的: 一个对象的状态在对象被创建之后就不再变化,这就是所谓的不变模式. 不变模式的结构 不变模式可增强对象的 ...

  5. [电脑常见问题] win8 ie浏览器打不开

    我安装的是win8专业版,正版的已经激活了,突然IE浏览器就打不开了,在桌面里面点IE没反应,在Metro界面点IE就回到开始界面 解决办法: 1.Win+R呼出运行窗口,键入Regedit,回车,打 ...

  6. python之fabric(一):环境env

    原文:https://my.oschina.net/indestiny/blog/289587 1. fabric有很多可配置的环境,如: user:默认用于ssh登录的本地用户名. password ...

  7. tomcat7的安装与配置、及Servlet部署

    一.tomcat7的下载页面 https://tomcat.apache.org/download-70.cgi 就是:32-bit Windows zip 下载得到apache-tomcat-7.0 ...

  8. Cocos2d-JS 自定义loading界面

    [转]http://blog.csdn.net/et_sandy/article/details/41415047 环境: win7 64位 Cocos2d-JS v3.1 Cocos Code ID ...

  9. ORACLE rowid切分大表

    通过如下sql获取rowid切分范围 ) || dbms_rowid.rowid_create(, DOI, lo_fno, lo_block, ) ) || ) || dbms_rowid.rowi ...

  10. Find the equipment indices

    Here is a simple program test task, it doesn't have very diffcult logic: A zero-indexed array A cons ...