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 ...
随机推荐
- jQuery MiniUI开发系列之:HTML标签配置
全部使用Javascript写一个界面,是一件很困难的事. 1)要求有较高的Javascript编程能力. 2)会造成“代码树”问题.一级又一级子"children",需要&quo ...
- delphi 调用c#dll
public interface iProduct { string Buy(); } [ClassInterface(ClassInterfaceType.None)] public class P ...
- 解决:jquery ajax非首次请求Server端获取cookie值中文乱码问题
HttpCookie cookie = new HttpCookie("RealName", HttpUtility.UrlEncode("你想要设置的值")) ...
- 【VB.NET】文本框快捷键支持
我们知道VB.NET中的文本框是不支持Ctrl+A的快捷键的. 如果让它支持呢? Private Sub txtSQL_KeyDown(ByVal sender As Object, ByVal e ...
- AngularJs 简单实现全选,多选操作
很多时候我们在处理CURD(增删改查)的时候需要实现批量操作数据,这时候就必须使用多选操作. Angular 中实现如下(当然还有很多种比笔者写的更好的方法,这里只是简单的实现.) demo 演示地址 ...
- 通过案例对 spark streaming 透彻理解三板斧之一: spark streaming 另类实验
本期内容 : spark streaming另类在线实验 瞬间理解spark streaming本质 一. 我们最开始将从Spark Streaming入手 为何从Spark Streaming切入 ...
- 异步编程:When.js快速上手
前些天我在团内做了一个关于AngularJS的分享.由于AngularJS大量使用Promise,所以我把基于Promise的异步编程也一并介绍了下.很多东西都是一带而过,这里再记录下. Angula ...
- hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- 计算机网络 学习笔记-传输层:TCP协议简介
概述: TCP传输前先要建立连接 TCP在传输层 点对点,一条TCP只能连接两个端点 可靠传输.无差错.不丢失.不重复.按顺序 全双工 字节流 TCP报文段 TCP报文段的报头前20字节是固定的,后面 ...
- 泛型的上限和下限的Demo
Main Class package Comparator.Bean; import java.math.BigDecimal; import java.util.List; import java. ...