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. BZOJ-3881:Divljak (AC自动机+DFS序+树链求并+树状数组)

    Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. “2 x” ...

  2. ACM学习历程—Hihocoder 1164 随机斐波那契(数学递推)

    时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 大家对斐波那契数列想必都很熟悉: a0 = 1, a1 = 1, ai = ai-1 + ai-2,(i > 1). ...

  3. win10系统下安装64位Oracle11g+LSQL Developer

    LSQL Developer作为强大的Oracle编辑工具,却只支持32bit,本文提供在安装用LSQL Developer打开64bitOracle的操作方法 工具/原料  oracle11g安装包 ...

  4. c# winform DataGridView 单元格的屏幕位置

    首先取得DataGridView的坐标位置:int dgvX = dataGridView1.Location.X;int dgvY = dataGridView1.Location.Y;然后取得选中 ...

  5. PHP 文件导出(Excel, CSV,txt)

    PHPExcel: 可以在我的文件中下载phpexcel放到项目中用!! 1,Excel 导出: /** * Excel导出例子 */ public function excel($res){ $ob ...

  6. 利用d3js绘出环形百分比环

    利用d3js绘出环形百分比环 (function() { var numberData = [{ value : 0.334, text : "33.4%", color : &q ...

  7. WPF访问UserControl的自定义属性和事件

    要实现外部窗体能直接访问UserControl的属性必须把UserControl的属性定义为依赖属性: 一,在UserControl.cs中为TextBox控件的Text建立依赖属性,输入" ...

  8. .Net锦囊-C#,.Net发送邮件三种方法…

    最近公司由于一个R&I项目的需要,用户要求在购买产品或出货等一些环节,需要发送邮件提醒或者说每周一让系统自动采集数据发送一封E-mail,因此我也就找来相关资料,写了一个Demo分享给大家,大 ...

  9. Hibernate&nbsp;Annotation&nbsp;(…

    引自:http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html 进入:http://www.hibernate.org 说明文档: ...

  10. 《精通Spring4.X企业应用开发实战》读后感第四章(Application中Bean的生命周期)

    package com.smart.beanfactory; import org.springframework.beans.BeansException; import org.springfra ...