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. CentOS 7 安装Dukto(局域网通信工具)

    rmp包 http://download.opensuse.org/repositories/home:/colomboem/CentOS_7/x86_64/dukto-6.0-13.1.x86_64 ...

  2. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  3. Ruby与Python开发的环境IDE配置(附软件的百度云链接)

    Ruby开发环境配置 1.Aptana_RadRails(提示功能不好,开发Ruby不推荐) 链接:http://pan.baidu.com/s/1i5q96K1 密码:yt04 2.Aptana S ...

  4. BJFU 1009

    描述 现在社会上的抽奖活动简直是太多了.前段时间中国联通就举办了一个很无聊的抽奖活动,规则是每人可以向中国联通的短信系统发送一个实数,系统每天会从这些数字中选择一个无重复(就是有且只有一个)且最小的数 ...

  5. 系统配置SQL profile

    select M.RESPONSIBILITY_NAME, B.PROFILE_OPTION_NAME, X.USER_PROFILE_OPTION_NAME, t.profile_option_va ...

  6. 夺命雷公狗---Thinkphp----4之数据表的设计

    我们这次来写的项目是仿http://yispace.net/39765.html而写的, 这里其实也就那回事,主要有标题和内容,和栏目, 文章页就更加的简单,其实也就那及格字段即可,我们分享得出的结果 ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle1

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetLineStyle1 procedure TForm1.Button1Click(Sender: TObject);var img : ...

  8. springmvc简述

    Spring Web MVC 是一种基于 Java 的实现了 Web MVC 设计模式的请求驱动类型的轻量级 Web 框架,即使用了 MVC 架构模式的思想,将 web 层进行职责解耦,基于请求驱动指 ...

  9. Office 2007在安装过程中出错-解决办法

    1, 可能是因为c:\program files\common files\microsoft Shared\web server Extensions\40\bin目录下缺少Fp4autl.dll, ...

  10. php CodeIgniter处理多环境错误级别配置

    php CodeIgniter处理多环境错误级别配置 开发者常常希望当系统运行在开发环境或生产环境中时能有不同的行为, 例如,在开发环境如果程序能输出详细的错误信息将非常有用,但是在 生产环境这将造成 ...