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. [HTML] CSS3 文本效果

    CSS3 文本效果 CSS3中包含几个新的文本特征. 在本章中您将了解以下文本属性: text-shadow word-wrap 浏览器支持

  2. No.5__C#

    One month 今天是个有纪念意义的日子,2015-4-23.今天是实习的第一个月,算是成就达成吧.虽然,除去了周末六日和清明什么的,只剩下20多天了,但是,还是好开心 啊,毕竟是第一次参加工作, ...

  3. (整理)IIS 7 503 "service unavailable" errors

    原文地址:http://mvolo.com/where-did-my-iis7-server-go-troubleshooting-503-quotservice-unavailablequot-er ...

  4. [VB.NET]取消按钮按下的默认事件响应

    大家应该有过这样的经历:有两个文本框a,b.通过编程,当我们在A中回车后,光标会移动到B文本框. 但是,不可避免的会听到一声“铛”的声音. 解决办法: 在Keydown或者KeyUp事件中,设置e.S ...

  5. SQLite数据库文件格式

    数据库命名约定 sqlite3_open()API用到数据库的文件名,可以是相对当前工作目录的相对路径名,也可以是从系统根文件树开始的完整路径名.任何被本地文件系统接受的正规文件名都是好的. 如果文件 ...

  6. SQL Server 之AdventureWorks 2008 安

    学习背景:<SQL Server 2008 编程入门经典> SQL Sever 版本 SQL Server 2008 R2 方法一: 1:AdventureWorks 2008 下载地址: ...

  7. 【转】git push 出现401 错误

    错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...

  8. HP XP7 GAD双活实现的理解

    XP7双活的虚拟卷global active device (GAD)实际上对应两个存储的两个物理卷(有点儿像Mirror Disk镜像) 当主机A向阵列A发出写数据请求后,阵列A首先检查要被写入的数 ...

  9. Windows 7 下如何设置机器级别的DCOM权限

    Windows 7 下如何设置机器级别的DCOM权限 To grant Remote Activation permissions to the SMS Admins group From the S ...

  10. js/json 数组的操作

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...