Description

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 41 2 3 4

题意:n*m的矩阵,可以做一行中的两个数交换,或者是列交换(每种最多一次),问可不可以还原成1-m的形式

解法:暴力,首先从第一行的1,2位置的数交换,交换后看错位的数量是不是小于等于2,大于2那么就算再交换一次也没办法还原了。直到有两个位置符合要求,则把这一次交换看成是列交换,(比如第一组数据)再根据这两个位置,将下一行也交换(因为我们是列交换嘛),判断是否符合小于等于2的条件,直到结束

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[200][200];
int b[200][200];
set<int>q,q2;
map<int,int>p;
map<int,int>::iterator it;
int cmd(int x,int y)
{
int flag=0;
for(int i=1;i<=n;i++)
{
int num=0;
swap(b[i][x],b[i][y]);
for(int j=1;j<=m;j++)
{
if(b[i][j]!=j)
{
num++;
}
}
// cout<<x<<" "<<y<<" "<<num<<endl;
if(num>2)
{
// cout<<"A"<<endl;
return 0;
}
}
return 1;
}
int main()
{
int flag=0;
cin>>n>>m;
for(int i=1; i<=n; i++)
{
int num=0;
for(int j=1; j<=m; j++)
{
cin>>a[i][j];
b[i][j]=a[i][j];
if(a[i][j]!=j)
{
num++;
p[j]++;
q.insert(j);
q2.insert(i);
}
}
if(num>2)
{
flag=1;
}
// cout<<num<<endl;
}
/* if(q2.size()!=n)
{
for(int i=1; i<=n; i++)
{
if(p[i]>=3)
{
cout<<"NO"<<endl;
return 0;
}
}
cout<<"YES"<<endl;
}
else if(q2.size()==n)
{
if(q.size()==2)
{
cout<<"YES"<<endl;
}*/ int flag1=0;
if(flag==0)
{
flag1=1;
}
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=m;j++)
{
if(cmd(i,j))
{
cout<<i<<" "<<j<<endl;
flag1=1;
}
for(int _=1;_<=n;_++)
{
for(int q=1;q<=m;q++)
{
b[_][q]=a[_][q];
}
}
}
}
if(flag1)
{
puts("YES");
}
else
{
puts("NO");
}
return 0;
}

  

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B的更多相关文章

  1. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

  2. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)D Dense Subsequence

    传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...

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

    链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing

    我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)

    题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...

  7. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)

    传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...

  8. 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 ...

  9. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A

    Description You are given names of two days of the week. Please, determine whether it is possible th ...

随机推荐

  1. 数据库SQL CRUD

    1.删除表 drop  table +表名 2.修改表 alter  table+表名+ add(添加)+列名+ int(类型) alter  table+表名+ drop(删除)+column(列) ...

  2. bzoj1758 [Wc2010]重建计划 & bzoj2599 [IOI2011]Race

    两题都是树分治. 1758这题可以二分答案avgvalue,因为avgvalue=Σv(e)/s,因此二分后只需要判断Σv(e)-s*avgvalue是否大于等于0,若大于等于0则调整二分下界,否则调 ...

  3. paper 79:MATLAB函数,interp1

    在matlab中有一个interp1()函数,可以帮助解决问题,具体情况如下:MATLAB中的插值函数为interp1,其调用格式为: yi= interp1(x,y,xi,'method') 其中x ...

  4. 整理的java的日期DateUtil

    package cn.knet.data.untils; import java.text.SimpleDateFormat; import java.util.Calendar; import ja ...

  5. 夺命雷公狗—angularjs—3—表单验证的高级用法

    其实我们的angularjs都是是块状代码,其实是可以在实际开发中保存下来以后就可以达到重复利用的目的了.. 废话不多说,直接上代码: <!doctype html> <html l ...

  6. 夺命雷公狗---Thinkphp----13之前台的头尾分离和导航分离

    我们在实际的开发中往往网站的头尾都是分离开来的,而且tp这点做的也很人性化,他给我们留了一个include标签可以直接引入网站的头尾部分. 我们要做的网站当然也不例外,头尾一样分离开来: 我们先用浏览 ...

  7. 在centos中编译putty时提示找不到gtk库的解决办法

    明明已经安过gtk了, 后来在csdn上发现了前人的解决办法 yum install libgnomeui-devel 现在编译出来的putty就可以有图形界面了

  8. 锋利的JQuery(一)

    释义: Ajax:Asynchronous Javascript And XML,异步的Javascript和XML 其它库: Prototype:最早 Dojo:学习曲线陡 YUI:比较丰富 Ext ...

  9. Openstack的dashboard开发之【浏览器兼容性】

    完全不支持浏览器: ie9(含)以下ie低版本浏览器及使用ie低版本浏览器的内核的扩展浏览器,如360安全浏览器(内核ie6) 原因:不支持vnc(需要浏览器支持才有vnc功能),jquery也不在支 ...

  10. 160902、Ionic、Angularjs、Cordova搭建Android开发环境

    1.jdk 环境变量配置 path:C:\Program Files\Java\jdk1.7.0_79\bin 2.node.js 因为安装cordova时要用到node.js的npm 下载地址: h ...