codeforces724B+Batch Sort
来源:互联网 发布:淘宝2016年销售总额 编辑:程序博客网 时间:2024/06/05 12:08
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:
Swap second and third columns. Now the table is
1 2 3 4
1 4 3 2
In the second row, swap the second and the fourth elements. Now the table is
1 2 3 4
1 2 3 4
每一行最多一次选择两个元素交换。最多一次机会选择两列,交换两列。问能不能让每一行都变成1,2,3。。。m。操作任意。
我们可以考虑交换列的操作放在哪里。交换后能不能让每一行都能在一次交换中变成1,2,3…m这种。。
那么我们就可以考虑1.2.3.。m的错位cnt有几位,当cnt==2||cnt==0时显然是可以在做多一次交换类让该行满足条件。。
所以先考虑不交换行,然后考虑i,j行交换。判断能否到达可行状态(每一行错位<=2)
#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<cstdlib>#include<queue>#include<vector>#include<map>#include<stack>#include<set>using namespace std;#define pi acos(-1.0)#define EPS 1e-6#define e exp(1.0); //2.718281828//log(x)#define mod 1000000007#define INF 0x7fffffff#define inf 0x3f3f3f3ftypedef long long LL;#define debug(x) cout<<x<<endl;#define debug2(x) cout<<x<<" ";const int maxn=100000+10;int mp[22][22];int temp[22][22];int n,m;bool judge(){ for(int i=1;i<=n;i++){ int cnt=0; for(int j=1;j<=m;j++){ if(mp[i][j]!=j){ cnt++; } } if(cnt>2) return false; } return true;}bool judge2(){ for(int i=1;i<=n;i++){ int cnt=0; for(int j=1;j<=m;j++){ if(temp[i][j]!=j){ cnt++; } } if(cnt>2) return false; } return true;}int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&mp[i][j]); } } if(judge()==true) cout<<"YES"<<endl;//不交换列 else{ bool ok=false; for(int i=1;i<=m;i++){ //交换列i,j列 for(int j=i+1;j<=m;j++){ memcpy(temp,mp,sizeof(mp)); for(int k=1;k<=n;k++) swap(temp[k][i],temp[k][j]); if(judge2()==true){ ok=true; break; } } if(ok) break; } if(ok) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / /======`-.____`-.___\_____/___.-`____.-'====== `=---='^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I have a dream!A AC deram!! orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz orz*/
- codeforces724B+Batch Sort
- Batch Sort
- [codeforces]B. Batch Sort 暴力
- codeforces 724B Batch Sort
- codeforces 724B Batch Sort
- CodeForces 724B Batch Sort
- 【39.77%】【codeforces 724B】Batch Sort
- Codeforces 724B Batch Sort(暴力枚举)
- Codeforces 724B Batch Sort【暴力枚举】
- 【codeforces 724B Batch Sort】+ 枚举
- Codeforces Round Intel Code Challenge Final Round B.Batch Sort
- batch
- Batch
- Batch
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- B. Batch Sort(暴力枚举)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B(模拟) && codeforce 724B Batch Sort
- sort
- powershell backdoor/trojan
- 读书笔记:C++ primer 5th edition--chapter10.泛型算法
- Java动态拼接字符串+改变多部分字体颜色+实现换行从第二行开头开始
- 主机找不到vmnet1和vmnet8,主机vmnet1vmnet8解决方法
- XMPP的基本使用以及Android即时通讯的基本思路
- codeforces724B+Batch Sort
- MySQL之——Replication的容量、故障排错以及多线程方式传输二进制日志
- 使用RecyclerView时item过多滑动报错
- requirejs和seajs那些蛋疼事儿
- android开发如何定制状态栏的颜色,同时支持4.4(KIKAT,api19)和19以上系统。
- linux下redis安装以及处理一些常见问题
- 修改数据表
- Python 包管理工具解惑
- 谈到电影,我们收获了什么