【codeforces 724B Batch Sort】+ 枚举
来源:互联网 发布:cnc编程软件排行 编辑:程序博客网 时间:2024/06/14 07:39
B. Batch Sort
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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
题目大意:
给你一个N*M的矩阵,你有两种操作:
①从一行中选择两个元素,将其交换。每一行只允许有这样的操作一次。
②选择两列,将两列的元素交换,这种操作只允许有一次。
目标矩阵:使得矩阵每一行都是从1到m的一个递增序列。保证输入的矩阵每一行的数据都是从1-m的。
思路:
因为将两列的元素交换这样的操作只有一次,那么我们首先暴力枚举出来两列,使得这两列元素进行交换之后,我们再进行每一行的判断。
上周的CF B题,居然今天才补上,果然还是菜的不行
#include<cstdio>#include<algorithm>using namespace std;int pa[22][22],N,M;bool solve(int a,int b){ int i,j,pl; for(i = 1 ; i <= N ; i++) swap(pa[i][a],pa[i][b]); for(i = 1 ; i <= N; i++){ pl = 0; for(j = 1 ; j <= M ; j++) if(pa[i][j] != j) pl++; if(pl > 2){ for(i = 1 ; i <= N ; i++) swap(pa[i][a],pa[i][b]); return false; } } return true;}int main(){ int i,j; scanf("%d%d",&N,&M); for(i = 1 ; i <= N; i++) for(j = 1 ; j <= M ; j++) scanf("%d",&pa[i][j]); for(i = 1 ; i <= M ; i++) for(j = i ; j <= M ; j++) if(solve(i,j)){ printf("YES\n"); return 0; } printf("NO\n"); return 0;}
- Codeforces 724B Batch Sort(暴力枚举)
- Codeforces 724B Batch Sort【暴力枚举】
- 【codeforces 724B Batch Sort】+ 枚举
- codeforces 724B Batch Sort
- codeforces 724B Batch Sort
- CodeForces 724B Batch Sort
- 【39.77%】【codeforces 724B】Batch Sort
- [codeforces]B. Batch Sort 暴力
- Codeforces Round Intel Code Challenge Final Round B.Batch Sort
- codeforces 724B 暴力+枚举
- CodeForces 724B(暴力枚举)
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) -- B. Batch Sort(暴力枚举)
- 枚举 codeforces 452B
- 【codeforces 711B + 枚举】
- Codeforces 652 B. z-sort
- 【Codeforces 652 B z-sort 】
- CodeForces 652B z-sort
- Batch Sort
- 对关系型数据库五个范式的理解
- If Slack But Ryver!
- 欢迎使用CSDN-markdown编辑器
- HDU4791——Alice's Print Service(线段树)
- 常见聚类算法分类
- 【codeforces 724B Batch Sort】+ 枚举
- 内存堆和栈的区别
- 加工路径的图形化编辑
- HDU 1286 找新朋友
- C#中遍历Hashtable的4种方法
- HDU 5033 Building (单调栈、计算几何)
- CC攻击的危害
- 安装完Windows Server 2008后怎么配置
- Android 关于网络以及数据处理的相关框架整理