传送门

Description

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.

Input

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.

Output

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

Sample Input

2 41 3 2 41 3 4 2
4 41 2 3 42 3 4 13 4 1 24 1 2 3
3 62 1 3 4 5 61 2 4 3 5 61 2 3 4 6 5

Sample Output

YES

NO

YES

思路

题意:给你一个NxM的矩阵块,每行都是数字1-M的一个排列,对于行,每行只允许交换两个数的位置一次,对于列,只允许交换两列一次,最多可交换次数为N + 1次,问有没有可能在允许的交换操作下使得每行都是上升序列。

数据范围很小,考虑暴力解。首先判断是否能只通过行操作,达到目的,不行的话,枚举列交换,然后在判断能否通过行操作达到目的。

 
#include<bits/stdc++.h>
using namespace std;
const int maxn = 25;

bool solve(int a[][maxn],int N,int M,int c1,int c2)
{
	int atmp[maxn][maxn];
	for (int i = 0;i < N;i++)	for (int j = 0;j < M;j++)	atmp[i][j] = a[i][j];
	for (int i = 0;i < N;i++)	swap(atmp[i][c1],atmp[i][c2]);
	for (int i = 0;i < N;i++)
	{
		int cnt = 0;
		for (int j = 0;j < M;j++)
		{
			if (atmp[i][j] != j + 1)	cnt++;
		}
		if (cnt > 2)	return false;
	}
	return true;
} 

int main()
{
	int N,M,a[maxn][maxn];
	while (~scanf("%d%d",&N,&M))
	{
		for (int i = 0;i < N;i++)	for (int j = 0;j < M;j++)	scanf("%d",&a[i][j]);
		bool flag = false;
		for (int i = 0;i < M - 1;i++)
		{
			for (int j = i + 1;j < M;j++)
			{
				flag = solve(a,N,M,i,j);
				if (flag)	break;
			}
			if (flag)	break;
		}
		if (flag)	printf("YES\n");
		else
		{
			int cnt = 0;
			for (int i = 0;i < N;i++)
			{
				cnt = 0;
				for (int j = 0;j < M;j++)
				{
					if (a[i][j] != j + 1)	cnt++;
					if (cnt > 2)	break;
				}
				if (cnt > 2)	break;
			}
			if (cnt > 2)	printf("NO\n");
			else	printf("YES\n");
		}
	}
	return 0;
}

  

 
 

Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)的更多相关文章

  1. Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)

    传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...

  2. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

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

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  5. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)

    题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...

  6. 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D

    http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...

  7. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划

    E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...

  8. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力

    D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...

  9. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学

    C. Ray Tracing 题目连接: http://codeforces.com/contest/724/problem/C Description oThere are k sensors lo ...

随机推荐

  1. 小记sql server临时表与表变量的区别

    临时表与表变量都可以起到“临时”的作用,那么两者主要的区别是什么呢? 这里不讨论创建方式,以及全局临时表.会话临时表这些,主要记录一下个人对两者的主要区别以及适用情况的看法,有什么不对或补充的地方,欢 ...

  2. Node 进阶:express 默认日志组件 morgan 从入门使用到源码剖析

    本文摘录自个人总结<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 章节概览 morgan是express默认的日志中间件, ...

  3. 知乎日报win10版 - 天天读报【开源】

    业余时间写的一个知乎日报win10版客户端,支持收藏,评论,点赞等. 商店地址:https://www.microsoft.com/zh-cn/store/apps/%E5%A4%A9%E5%A4%A ...

  4. bootstrap点滴

    1.nav-stacked 这个属性可以决定 tab的变为竖的,不添加的话为横向的. 2.tab  横向的 ul中必须含有nav nav-tabs ul li a 中必须有data-toggle=&q ...

  5. SQLite安装、编译与应用

    什么是 SQLite SQLite是一款轻量级的.基于文件的嵌入式数据库,实现自包容.零配置.支持事务的SQL数据库引擎.与其他数据库管理系统不同,SQLite 的安装和运行非常简单,在大多数情况下, ...

  6. nios II--实验4——按键中断硬件部分

    按键中断 硬件开发 新建原理图 1.打开Quartus II 11.0,新建一个工程,File -> New Project Wizard…,忽略Introduction,之间单击 Next&g ...

  7. C语言字符串与数字相互转换

    在C/C++语言中没有专门的字符串变量,通常用字符数组来存放字符串.字符串是以“\0”作为结束符.C/C++提供了丰富的字符串处理函数,下面列出了几个最常用的函数. ● 字符串输出函数puts. ● ...

  8. SharePoint 站点集和子站点数据互相读取

    1.站点集中可以使用SPSite.AllWeb,然后遍历所有站点的isRootWeb,根据siteTemplate取得需要的子站点. /// <summary> /// Handles t ...

  9. 1027mysqlbinlog工具日志恢复

    -- 转自http://bbs.csdn.net/topics/310068149-- 其实就是找到对应的位置然后执行语句即可 -- 第一部分 输出mysqlbinlog "C:\Progr ...

  10. 浅谈SEO翻倍提升网站流量

    本文是在简单了解并且熟悉SEO技术的基础上加上自己的总结归纳,对于SEO还是一个长期的工程不是一天就能一下优化排名第一.下面就是我对SEO的一些见解: 一.关键字选择 内容相关 搜索次数多,竞争小 主 ...