传送门

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. 学习笔记——Maven 如何处理传递性依赖

    maven引入的传递性依赖机制,一方面大大简化和方便了依赖声明,另一方面,大部分情况下我们只需要关心项目的直接依赖是什么,而不用考虑这些直接依赖会引入什么传递性依赖.但有时候,当传递性依赖造成问题的时 ...

  2. Tomcat 启动提示未发现 APR 的解决方法

    Tomcat 启动出现信息如下: 信息: The APR based Apache Tomcat Native library which allows optimal performance in ...

  3. Android开发自学笔记—1.1(番外)AndroidStudio常用功能介绍

    一.界面区介绍 1.项目组织结构区,用于浏览项目文件,默认Project以Android组织方式展示. 2.设计区,默认在打开布局文件时为设计模式,可直接拖动控件到界面上实现所见即所得,下方的Desi ...

  4. 【Alpha版本】冲刺阶段——Day 9

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  5. css3实现的3中loading动画效果

    一.css3中animation动画各种属性详解: animation Value:     [<animation-name> || <animation-duration> ...

  6. js获取上传的文件并用ajax提交

    <form id="form1" name="form1" encType="multipart/form-data" method= ...

  7. Android点击EditText文本框之外任何地方隐藏键盘的解决办法

    1,实现方法一:通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),在事件里进行键盘隐藏 <LinearLayout xmlns:android=&q ...

  8. 【转】深入浅出Java三大框架SSH与MVC的设计模式

    原文链接:http://www.cnblogs.com/itao/archive/2011/08/22/2148844.html 在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架: ...

  9. HTML5文件系统API和资料整理

    来着火狐开发网络的官方文档:点我打开 : W3C的官方文档: 点我打开 : 园友的博客:  点我打开: 浏览器兼容性, 好了就chrome支持, 我刚刚更新的火狐37也不支持, nice, 太nice ...

  10. 网络流最小割 POJ 3469

    题意  2个CPU n个任务 给出在第一个 第二个运行时的花费 m  个  a  b 不在同一个CPU运行的额外花费 建图 源点 ->   n    -> 汇点 权          a1 ...