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. (转)epoll非阻塞读写规则

    EPOLL技术 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随 ...

  2. form 提交数据编码梳理

    之前对form单提交的操作一直都是迷迷糊糊,知道怎么用,但是随着ajax2的出现,我们有更多的方式操作form表单提交,但是底层的原理我们要好好的做个梳理. 常见的form提交有post和get这两种 ...

  3. HihoCoder1642 : 三角形面积和([Offer收割]编程练习赛37)(求面积)(扫描线||暴力)(占位)

    描述 如下图所示,在X轴上方一共有N个等腰直角三角形.这些三角形的斜边与X轴重合,斜边的对顶点坐标是(Xi, Yi). (11,5) (4,4) /\ /\(7,3) \ / \/\/ \ / /\/ ...

  4. 每天一个linux命令(3):cd命令

    版权声明 更新:2017-04-27博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下命令cd. 2 开 ...

  5. THUPC2019划水记

    虽然早就打不动了,虽然一个队友提前说好跑路了,还是两个人来玩了玩.最大的失误是没有开场打模拟题,然后就没骗到钱,还是要向某一心骗钱不顾排名的队伍学习.这次的模拟题超简单,很愉快地就打完了,也没调多久, ...

  6. ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

  7. poj2356Find a multiple——鸽巢定理运用

    题目:http://poj.org/problem?id=2356 N个数,利用鸽巢定理可知应有N+1个前缀和(包括0),因此其%N的余数一定有重复: 同余的两个前缀和之差一定为N的倍数,据此得出答案 ...

  8. 基于Qt Gui的Led控制显示程序

    基于arm + linux的嵌入式软件开发,基本上的内容主要是:u-boot的移植,kernel的裁剪和相关驱动程序的设计,root-fs的制作,应用程序的设计,其中,应用程序主要包含两方面的内容:G ...

  9. 【旧文章搬运】VC插件中如何获取当前工程的工作目录

    原文发表于百度空间,2014-09-24========================================================================== 好难找的资 ...

  10. 安装pillow

    最近想学Python的图像操作.首要任务就是安装pillow.这个强大的图形处理工具. 但是我遇到了一个问题. Collecting pilow  Could not find a version t ...