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. 源码解读—HashTable

    在上一篇学习过HashMap(源码解读—HashMap)之后对hashTable也产生了兴趣,随即便把hashTable的源码看了一下.和hashMap类似,但是也有不同之处. public clas ...

  2. Chap3: question: 11 - 18

    11. double 数值的整数次方 note: 浮点数表示时有误差,判等时必须自己根据精度要求实现. #include <iostream> #include <ctime> ...

  3. 9. Sort List && Insertion Sort List (链表排序总结)

    Sort List Sort a linked list in O(n log n) time using constant space complexity.                   H ...

  4. IIS7 IIS7.5 IIS8.5 HTTP 错误 500.19 – Internal Server Error解决方案小记

    今天配置IIS(win8.1 IIS8.5)的web.config出现如下错误: HTTP 错误 500.19 – Internal Server Error无法访问请求的页面,因为该页的相关配置数据 ...

  5. 框架操作DOM和原生js操作DOM比较

    问题引出 对于Angular和React操作DOM的速度,和原生js操作DOM的速度进行了一个比较: 一个同学做的demo 代码如下: <!DOCTYPE html> <html n ...

  6. C#通过安全证书生成签名和验签辅助类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  7. android开发--布局三(微信布局)

    微信主界面 我们布局应该从局来看,如上图,我们可以分为三个大的LinearLayout,再从LinearLayout嵌套其它布局,从而做出界面 文件 主界面代码: <LinearLayout x ...

  8. JavaScript入门

    本篇内容是学习慕课网相关课程后,总结出可能未来会忘记的内容 (一)JavaScript入门操作 1.js代码插入位置,以及执行顺序 <head> <script type=" ...

  9. 比较一下Linux下的Epoll模型和select模型的区别

    一. select 模型(apache的常用) 1. 最大并发数限制,因为一个进程所打开的 FD (文件描述符)是有限制的,由 FD_SETSIZE 设置,默认值是 1024/2048 ,因此 Sel ...

  10. C#之Winform中treeview控件绑定数据库

    private DataSet ds; private SqlDataAdapter sqlDataAdapter1; private int maxnodeid; private void Form ...