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 <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,c[][],vis[][],num[];
struct node
{
int cnt,id;
bool operator<(const node&p)const
{
return cnt>p.cnt;
}
}ca[];
bool flag;
bool solve(int a,int b)
{
int d[][];
int i,j;
rep(i,,n)rep(j,,m)d[i][j]=c[i][j];
rep(i,,n)swap(d[i][a],d[i][b]);
rep(i,,n)
{
int now=;
rep(j,,m)if(d[i][j]!=j)now++;
if(now>)return false;
}
return true;
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)ca[i].id=i;
rep(i,,n)rep(j,,m)
{
scanf("%d",&c[i][j]);
if(c[i][j]!=j)ca[i].cnt++;
}
sort(ca+,ca+n+);
if(ca[].cnt<=)puts("YES");
else if(ca[].cnt>)puts("NO");
else
{
j=;
int pos=ca[].id;
rep(i,,m)if(c[pos][i]!=i)num[j++]=i;
rep(i,,)rep(j,i+,)
{
if(solve(num[i],num[j]))return *puts("YES");
}
puts("NO");
}
//system("Pause");
return ;
}
Batch Sort的更多相关文章
- CF724B. Batch Sort[枚举]
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CodeForces 742B Batch Sort
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 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 ...
- 【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 ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- codeforces724-B. Batch Sort
想着想着就忘了有什么问题没解决,坑啊 一开始读错题意了,而且一着急写了两大段差不多的代码,冗余度啊,不说了.. 显然的一点,给的数据是绝对离散的,每行都是1~m的排列 难点一.如何移动能使未排序的数组 ...
- [CF724B]Batch Sort(暴力,思维)
题目链接:http://codeforces.com/contest/724/problem/B 题意:给出n*m的数字阵,每行数都是1-m的全排列,最多可以交换2个数一次,整个矩阵可以交换两列一次. ...
随机推荐
- JavaScript事件响应的基础语法总结
1.onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件.同时onclick事件调用的程序块就会被执行,通常与按钮一起使用 //例子 <html> <head> ...
- Excel教程(10) - 逻辑运算符
AND 用途:所有参数的逻辑值为真时返回 TRUE(真):只要有 一个参数的逻辑值为假,则返回 FALSE(假). 语法:AND(logical1,logical2, ). 参数:Logical1, ...
- Excel教程(7) - 工程函数
BESSELI 用途:返回修正 Bessel 函数值,它与用纯虚数参数运算 时的 Bessel 函数值相等. 语法:BESSELI(x,n) 参数:X 为参数值.N 为函数的阶数.如果 n 非 ...
- Chapter 16_2 继承
类也是对象,所有它们也可以从其他类获得方法.这就是“继承”,可以在Lua中表示: Account = { balance = } function Account:new(o) o = o or {} ...
- varnish屏蔽control+F5导致缓存失效
刚刚接触Varnish缓存,对静态资源进行缓存.目前问题,当浏览器Control+F5刷新页面,导致缓存失效. 参照:http://zhangxugg-163-com.iteye.com/blog/1 ...
- oracle 备份操作流程
Oracle 库表导出步骤 例如,要导出wcsr用户下的所有表,已知用户名/密码:wcsr/wcsr_woer 首先打开cmd.exe 其次创建备份目录,最好目录不包含空格和中文名 md d:\ora ...
- Sublime 3 and Python
1. 安装Anaconda插件 Ctrl+Shift+P打开控制面板,找到Install Package,回车 在弹出框中输入Anaconda,回车安装即可. 2.配置Anaconda文件 Prefe ...
- 框架基础:ajax设计方案(一)---集成核心请求
报告,我要说话!xp被历史淘汰了,IE6 say goodbye了,太TM开心了,从此不要兼容IE6了,哈哈哈哈哈哈 报告,我要说话!IE这sb为啥不早点被杀掉呢,找工作听说要兼容IE,立马软了,唉唉 ...
- Chapter 2 Open Book——8
But as far as I could tell, life worked that way most of the time. 但是即使我这么说,生活大多数时间还是这样的. 但就我所能告诉你的, ...
- ASP.NET ValidationGroup 属性和CssClass 属性
定义和用法 获取或设置在 Button 控件回发到服务器时要进行验证的控件组. 通常在表单中存在多个按钮时使用该属性. 语法 <asp:Button ValidationGroup=" ...