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

貌似交换行后再交换两个列不会得到更优解
爆枚交换哪两列然后每行贪心
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
const int N=,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n,m,a[N][N];
bool check(){
for(int i=;i<=n;i++){
int cnt=;
for(int j=;j<=m;j++) if(a[i][j]!=j) cnt++;
if(cnt>) return false;
}
return true;
}
int main(){
n=read();m=read();
for(int i=;i<=n;i++)
for(int j=;j<=m;j++) a[i][j]=read(); if(check()) {puts("YES");return ;}
for(int i=;i<=m;i++)
for(int j=i+;j<=m;j++){
for(int z=;z<=n;z++) swap(a[z][i],a[z][j]);
if(check()) {puts("YES");return ;}
for(int z=;z<=n;z++) swap(a[z][i],a[z][j]);
}
puts("NO");
}
 

CF724B. Batch Sort[枚举]的更多相关文章

  1. [CF724B]Batch Sort(暴力,思维)

    题目链接:http://codeforces.com/contest/724/problem/B 题意:给出n*m的数字阵,每行数都是1-m的全排列,最多可以交换2个数一次,整个矩阵可以交换两列一次. ...

  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. CodeForces 742B 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. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort

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

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

  8. B. Batch Sort

    http://codeforces.com/contest/724/problem/B 被坑了,一开始以为如果有一行已经是排好序了,然后有一行需要转换的次数 >= 2的话,那就直接no了. 因为 ...

  9. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

随机推荐

  1. Java并发编程:Thread类的使用

    Java并发编程:Thread类的使用 在前面2篇文章分别讲到了线程和进程的由来.以及如何在Java中怎么创建线程和进程.今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知 ...

  2. c# json序列化 意外字符i 意外字符ï 解决方案

    今天使用DataContractJsonSerializer遇到了这个问题 这是个蛋疼的问题,折腾了我好久,反复检查对象和json字符串,没发现什么问题,而且错误提示还看走眼了,是ï不是i 现公布解决 ...

  3. UEditor编辑器的使用

    1.首先我们要去官网下载UEditor编辑器,选择语言,这里我用的是php utf-8版本(李昌辉) 2.下载完成之后解压文件,将解压的文件放到我们的网站目录里面 3.ueditor/utf8-php ...

  4. Web AppBuilder Widget使用共享类库的方式

    Web AppBuilder是Esri公司推出的快速WebGIS应用搭建工具,具有以下特性: 不需要编程,快速创建应用 WYSIWYG 交互式应用 支持2D和3D应用 基于ArcGIS API for ...

  5. ios 提取html 字符串中的img 的地址(图片地址)

    本文原文地址 http://www.cnblogs.com/qianLL/p/6082287.html 有时候 后台返回的是一串html'字符串 我们需要把里面的图片地址提取出来  这个关键就是一个正 ...

  6. iOS面试题总结 (三)

    22 键值编码KVC KVC全称key valued coding 键值编码 提到KVC,就不能不提反射机制,反射机制就是在运行状态中,对于任意一个类,都能够调用他的所有属性和方法,对于任意一个对象, ...

  7. 开始使用 UIAlertController 吧

    UIAlertView 与 UIActionSheet UIAlertView 样式 实现 - (void)showAlertView { self.alertView = [[UIAlertView ...

  8. netstat监控大量ESTABLISHED连接与Time_Wait连接问题

    问题描述: 在不考虑系统负载.CPU.内存等情况下,netstat监控大量ESTABLISHED连接与Time_Wait连接. # netstat -n | awk '/^tcp/ {++y[$NF] ...

  9. 基于Ruby的watir-webdriver自动化测试方案与实施(二)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(一) http://www.cnblogs.com/Javame/p/4159360.html 继续 ... ...   回顾 软 ...

  10. C#命名规则和编码规范

    用Pascal规则来命名属性.方法.事件和类名. public class HelloWorld { public void SayHello(string name) { } } Pascal规则是 ...