题目链接:http://codeforces.com/problemset/problem/724/B

题目大意:

  给出N*M矩阵,对于该矩阵有两种操作:

  (保证,每行输入的数是 1-m 之间的数且不重复)

  1.交换两列,对于整个矩阵只能操作一次

  2.每行交换两个数。

  交换后是否可以使每行都是1-m 数字递增。

解题思路:

  1.得到矩阵后先判断,是否每行可以交换两个数可以得到递增的矩阵,如果可以则输出“YES”.

  2.暴力交换两列,交换两列后,判断每行是否可以交换两个数得到递增的矩阵,如果可以则输出“YES”.

    [注意:如果交换之后不可得到递增的矩阵,要记得将交换后的两列交换回来]

AC Code:

 #include<bits/stdc++.h>
using namespace std; const int N=;
int na[N][N];
int n,m; int judge(int na[N][N])
{
for(int i=; i<=n; i++)
{
int cut=;
for(int j=; j<=m; j++)
if(na[i][j]!=j)cut++;
if(cut>)return ;
}
return ;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&na[i][j]);
if(judge(na))
{
printf("YES\n");
continue;
}
int flag=;
for(int i=; i<=m; i++)
{
if(flag)break;
for(int j=i+; j<=m; j++)
{
if(flag)break;
for(int x=; x<=n; x++)swap(na[x][i],na[x][j]);
if(judge(na))flag=;
for(int x=; x<=n; x++)swap(na[x][i],na[x][j]);
}
}
printf("%s\n",flag?"YES":"NO");
}
return ;
}

codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)的更多相关文章

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

  2. CodeForces 742B Batch Sort

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

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

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

  4. Batch Sort

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

  5. CF724B. Batch Sort[枚举]

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

  6. codeforces724-B. Batch Sort

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

  7. Sort 多列正排序,倒排序

    linux sort 多列正排序,倒排序 转自https://segmentfault.com/a/1190000005713784  发布于 2016-06-14  sort是在Linux里非常常用 ...

  8. PIVOT就是行转列,UNPIVOT就是列传行

    PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合.UNPIVOT与PIVOT执行相反的操作,将表值表达式的列转换为列值. 通 ...

  9. sql server显示某一列中有重复值的行

    sql server查询一张表 ,显示某一列中有重复值的行,可以这样写: Select * From 表名 where 列名 in(Select 列名 From Table group by 列名 h ...

随机推荐

  1. android部分开发摘要

    Async 异步  不会阻塞当前线程sync  同步 数据库是应用软件|结构化数据存储  JDBC  SQL ellipsis 省略 content provider   URI thread--lo ...

  2. [转]扩展RBAC用户角色权限设计方案

    原文地址:http://www.iteye.com/topic/930648 RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地 ...

  3. 转: linux内核版本本地版本号的检查——setlocalversion

    转载:http://blog.csdn.net/adaptiver/article/details/7225980 1.   引子 编译2.6.35.7 kernel版本的时候发现,"2.6 ...

  4. Kernel Methods (4) Kernel SVM

    (本文假设你已经知道了hard margin SVM的基本知识.) 如果要为Kernel methods找一个最好搭档, 那肯定是SVM. SVM从90年代开始流行, 直至2012年被deep lea ...

  5. poj1523 求割点 tarjan

    SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7678   Accepted: 3489 Description C ...

  6. hdu3555 数位dp

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  7. Visual Studio 2015正式版/产品密钥

    Visual Studio 2015  发行说明: https://visualstudio.com/zh-cn/news/vs2015-vs.aspx Visual Studio  2015 特性简 ...

  8. Leetcode # 169, 229 Majority Element I and II

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. SQL Server附加数据库时失败,提示:“未重新生成日志,因为有不止一个日志文件”

    这个只能是试一下的方法,但不一定能成功,可以尝试如下几个方法: 1.登录远程桌面,然后以.登录SQL Server,并以Windows身份登录,然后再附加数据库时把日志文件删除. 2.试下这个脚本: ...

  10. [NOIP2013] 提高组 洛谷P1969 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...