http://codeforces.com/contest/724/problem/B

被坑了,一开始以为如果有一行已经是排好序了,然后有一行需要转换的次数 >= 2的话,那就直接no了。

因为一开始以后转换次数>=2必定需要用了转换列,然后就搞乱了排序好的哪一行,但是排序好的那一行可以用交换两个数来修复。

2 4

1  2 3 4

2 1  4 3

然后其他的思路就是,①、有一个需要转换3次以上的就不行了。

②、随便找一个转换次数为2的,开始暴力枚举他们两两用列转换,然后再判断是否全部只需要一次转换就行了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = + ;
int a[maxn][maxn];
int n, m;
int mx;
bool check() {
bool flag = false;
for (int i = ; i <= n; ++i) {
int j;
for (j = ; j <= m; ++j) {
if (a[i][j] != j) {
break;
}
}
if (j == m + ) {
flag = true;
break;
}
}
if (flag == false) return false;
for (int i = ; i <= n; ++i) {
int t = ;
for (int j = ; j <= m; ++j) {
if (a[i][j] != j) ++t;
}
// if (t >= 3) return true;
mx = max(t, mx);
}
if (mx >= ) return true;
return false;
}
int row[maxn];
void getrow() {
for (int i = ; i <= n; ++i) {
row[i] = ;
for (int j = ; j <= m; ++j) {
row[i] += a[i][j] != j;
}
}
}
bool allone() {
for (int i = ; i <= n; ++i) {
if (row[i] >= ) return false;
}
return true;
}
int gg[maxn];
void get(int aa, int bb) {
for (int i = ; i <= n; ++i) {
swap(a[i][aa], a[i][bb]);
}
}
void show() {
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void work() {
cin >> n >> m;
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
cin >> a[i][j];
}
} if (check() || mx >= ) {
cout << "NO" << endl;
return;
}
getrow();
if (allone()) {
cout << "YES" << endl;
return;
}
int pos;
// show();
for (int i = ; i <= n; ++i) {
if (row[i] >= ) {
pos = i;
break;
}
}
int lengg = ;
for (int i = ; i <= m; ++i) {
if (a[pos][i] != i) {
gg[++lengg] = i;
}
}
// for (int i = 1; i <= m; ++i) {
// cout << gg[i] << " ";
// }
// cout << endl;
for (int i = ; i <= lengg; ++i) {
for (int j = i + ; j <= lengg; ++j) {
get(gg[i], gg[j]);
getrow();
if (allone()) {
cout << "YES" << endl;
return;
}
get(gg[i], gg[j]);
}
}
cout << "NO" << endl;
return;
}
int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}

B. Batch Sort的更多相关文章

  1. CF724B. Batch Sort[枚举]

    B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. CodeForces 742B Batch Sort

    B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Batch Sort

    Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  4. 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 ...

  5. 【39.77%】【codeforces 724B】Batch Sort

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort

    链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...

  7. 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 ...

  8. codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)

    题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...

  9. codeforces724-B. Batch Sort

    想着想着就忘了有什么问题没解决,坑啊 一开始读错题意了,而且一着急写了两大段差不多的代码,冗余度啊,不说了.. 显然的一点,给的数据是绝对离散的,每行都是1~m的排列 难点一.如何移动能使未排序的数组 ...

  10. [CF724B]Batch Sort(暴力,思维)

    题目链接:http://codeforces.com/contest/724/problem/B 题意:给出n*m的数字阵,每行数都是1-m的全排列,最多可以交换2个数一次,整个矩阵可以交换两列一次. ...

随机推荐

  1. hdu 6109 数据分割

    /** * 题目描述有点坑,勉强能读懂,大致意思,有多组约束条件.原本每组数据之间是有分界符号的 * 现在分界符号没了,让你找出原来每组数据多少个条件,并且告诉,每组的最后一个条件会使得与前面的 * ...

  2. 苹果手机app试玩平台汇总--手机链接入口

    注意: 点击下载,根据提示步骤走即可. 下载后绑定手机号和微信后.才能提现 每天3点更新任务,4点最多! | 平台 | 提现额 | 任务量| 推荐强度 | 下载 | 1.小鱼,10元,大量,强推! → ...

  3. MySQL交叉表处理_20160923

    交叉表处理,在二维表中例如下面表 想把年月字段放到列字段,在sql中可以使用sum(if(条件,求和字段,null)) 函数来进行行列的转置 1.首先是上篇的年月字段在一列 SELECT city A ...

  4. bzoj 3522: Hotel dfs

    题目大意 在无边权树上求三个点,使两两点的距离等.求方案数\((n\leq 5000)\) 题解 我们知道三个点在树上的关系只有两种 三点共链 三点不共连 (这不是废话吗) 我们发现三点共链肯定不满足 ...

  5. ESFramework Demo -- P2P通信Demo(附源码)

    现在我们将在ESFramework Demo -- 文件传送Demo 的基础上,使用ESPlus提供的第四个武器,为其增加P2P通信的功能.在阅读本文之前,请务必先掌握ESFramework 开发手册 ...

  6. PC lint -sem 用法示例

    -sem(std::auto_ptr::auto_ptr,custodial(1)) // the auto_ptr class type // handles custody automagical ...

  7. 微信公众平台:扫一扫demo

    ylbtech-微信公众平台:扫一扫demo 1.返回顶部 1.Web.config <appSettings> <add key="appid" value=& ...

  8. ngd 查看ng2应用的组件树、路由树

    1.全局安装ngd npm install -g @compodoc/ngd-cli 2.在tsconfig的同级目录下使用ngd命令 ngd

  9. NGUI panel 之下widget最大depth是1000,超过1000时OnClick会出问题!

    经过我的测试发现ngui widget的depth是有限制的!原本以为只要不同panel间的depth设置好了后无论widget depth如何设置都没问题,直到我们项目中出现奇怪的点击问题后才发现这 ...

  10. STM in Clojure

    Transactional memory in Clojure is implemented using Multiversion Concurrency Control protocol http: ...