CodeForces 742B 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
In the first sample, one can act in the following way:
- Swap second and third columns. Now the table is1 2 3 41 4 3 2
- In the second row, swap the second and the fourth elements. Now the table is1 2 3 4
- 开始没做出来 后来看了别人博客 发现还是不难的 暴力就可以了,先交换两列,然后判断每行不递增的有几个,一旦超过两个的
- 交换另一列 当然刚交换的要恢复原样
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int a[][];
int m,n;
int fun(){
int i,j;
for(i=;i<=n;i++){
int ans=;
for(j=;j<=m;j++){
if(a[i][j]!=j){
ans++;
}
}
if(ans>)
return ;
}
return ;
}
int main(){
int i,j,k;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++){
for(j=;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
if(m==){
printf("YES\n");
return ;
}
if(fun()){
printf("YES\n");
return ;
}
for(i=;i<=m;i++){
for(j=i+;j<=m;j++){
for(k=;k<=n;k++){
swap(a[k][i],a[k][j]);
}//cout<<fun()<<endl;
if(fun()==){
printf("YES\n");
return ;
}
for(k=;k<=n;k++){
swap(a[k][i],a[k][j]);
}
}
}
printf("NO\n");
}
CodeForces 742B Batch Sort的更多相关文章
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- 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 ...
- CF724B. 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 ...
- 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 258div2 B Sort the Array
题目链接:http://codeforces.com/contest/451/problem/B 解题报告:给出一个序列,要你判断这个序列能不能通过将其中某个子序列翻转使其成为升序的序列. 我的做法有 ...
- [CF724B]Batch Sort(暴力,思维)
题目链接:http://codeforces.com/contest/724/problem/B 题意:给出n*m的数字阵,每行数都是1-m的全排列,最多可以交换2个数一次,整个矩阵可以交换两列一次. ...
- codeforces 340D Bubble Sort Graph(dp,LIS)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Bubble Sort Graph Iahub recently has lea ...
随机推荐
- 【javascript基础】3、变量和作用域
前言 这篇和大家说一下javascript中的变量和作用域,由于是将基础嘛,主要给大家捋一下知识,不想翻开书复习的道友可以看一下,打算刚开始学习javascript的同学可以扫一眼. PS:jQuer ...
- 透视校正插值(Perspective-Correct Interpolation)
在渲染器光栅化每个三角形的过程中,需要对根据顶点属性对三角形进行扫描线插值.此时由于投影面上顶点的2D坐标与顶点属性不成线性关系,因此是不能简单地使用线性插值来计算顶点属性的. 此时应当利用透视校正插 ...
- Blackfin DSP(二):寄存器操作与GPIO
BlackfinDSP的寄存器是通过指针操作的,与51.ARM等MCU一样,通过“或”操作来置1,通过“与”操作清零. 在DSP上最简单的外设非IO口莫属,但是由于其功能强大,远非一般IO口可比,因此 ...
- Jmeter组件2. Timer 定时器
关于定时器,首先明确几个概念 定时器在每个Sampler执行之前执行 定时器有作用域,同一个作用域内的定时器会在域内Sampler执行之前都执行掉 如果要让某定时器只作用于一个Sampler,将定时器 ...
- [LINUX] 查看连接数和IO负载
Iostat 是 sysstat 工具集的一个工具,需要安装. Centos的安装方式是: yum install sysstat Ubuntu的安装方式是: aptitude install sys ...
- Flask First Look
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "He ...
- Android 环境常见问题
1.启动adb时报: adb server is out of data.kill... ADB server didn't ACK *failed to start daemon* er ...
- 30、shiro框架入门2,关于Realm
1.Jdbc的Realm链接,并且获取权限 首先创建shiro-jdbc.ini的配置文件,主要配置链接数据库的信息 配置文件中的内容如下所示 1.变量名=全限定类名会自动创建一个类实例 2.变量名. ...
- oracle net manager的配置文件tnsnames.ora位置
配置文件所在的路径:C:\app\Ling-PC\product\11.2.0\client_1\network\admin (红色为安装的盘符位置)
- C语言中的union
1.union中可以定义多个成员,union的大小由最大的成员的大小决定. 2.union成员共享同一块大小的内存,一次只能使用其中的一个成员,与struct形成鲜明对比. 3.对某一个成员赋值,会覆 ...