CF724B. Batch Sort[枚举]
2 seconds
256 megabytes
standard input
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.
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.
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).
2 4
1 3 2 4
1 3 4 2
YES
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
NO
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
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[枚举]的更多相关文章
- [CF724B]Batch Sort(暴力,思维)
题目链接:http://codeforces.com/contest/724/problem/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 ...
- CodeForces 742B Batch Sort
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Batch Sort
Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 【39.77%】【codeforces 724B】Batch Sort
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort
链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...
- 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 ...
- B. Batch Sort
http://codeforces.com/contest/724/problem/B 被坑了,一开始以为如果有一行已经是排好序了,然后有一行需要转换的次数 >= 2的话,那就直接no了. 因为 ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
随机推荐
- SQL 优化总结
SQL 优化总结 (一)SQL Server 关键的内置表.视图 1. sysobjects SELECT name as '函数名称',xtype as XType FROM s ...
- 未能解析此远程名称: 'api.ucpaas.com'
未能解析此远程名称: 'api.ucpaas.com' 这个问题的原因不是云之讯,而是(我用的是阿里云)云服务器的DNS解析的问题 或者是云服务器后台的安全组规则的问题, 应该把内网入方向和内网出 ...
- VirtualBox动态添加虚拟硬盘
本文非技术类文章,仅作为记录. 因为使用VirtualBox时遇到这样的问题:原本虚拟机分配存储的80G已经足够了,但是随着使用的文件越来越多,需要的空间也越来越大,因此不得不扩展虚拟机的磁盘容量. ...
- Android源码编译make的错误处理
android源码下载:官方下载 或参考android源码下载方式 Android编译版本: PLATFORM_VERSION=4.0.1(最新Android 4.0.1) OS 操作系统平台: Li ...
- 用Kotlin改写PHP程序是什么样的体验
学Kotlin其实要看:http://kotlinlang.org/docs/kotlin-docs.pdf 在线版是不完整的!!!少了一些章节,会有点难看懂后面的文档. 我选择了WordPress里 ...
- 变通实现微服务的per request以提高IO效率(二)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- CSS3与页面布局学习笔记(六)——CSS3新特性(阴影、动画、渐变、变形( transform)、透明、伪元素等)
一.阴影 1.1.文字阴影 text-shadow<length>①: 第1个长度值用来设置对象的阴影水平偏移值.可以为负值 <length>②: 第2个长度值用来设置对象的阴 ...
- 鼠标悬停显示CSS3动画边框
效果体验:http://keleyi.com/keleyi/phtml/css3/14.htm 以下是代码: <!DOCTYPE html> <html xmlns="ht ...
- 彻底解决mysql中文乱码的办法 ???
MySQL会出现中文乱码的原因不外乎下列几点:1.server本身设定问题,例如还停留在latin12.table的语系设定问题(包含character与collation)3.客户端程式(例如p ...
- 委托的N种写法,你喜欢哪种?
一.委托调用方式 1. 最原始版本: delegate string PlusStringHandle(string x, string y); class Program { static void ...