[codeforces]B. Batch Sort 暴力

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

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

Examples
input
2 4
1 3 2 4
1 3 4 2
output
YES
input
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
output
NO
input
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
output
YES
Note

In the first sample, one can act in the following way:

  1. Swap second and third columns. Now the table is
    1 2 3 4
    1 4 3 2
  2. In the second row, swap the second and the fourth elements. Now the table is
    1 2 3 4
    1 2 3 4
大意:每行最多只能执行 交换两个数字 一次 [可不交换],还可以至少交换两列,[可不交换]
求其是否可以实现每一行都为从1到m递增


思路:暴力,先交换每一个,然后判断每行经过一次交换,是否能达到该状态

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 10007
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define PI acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int a[100][100];
int judge();
int n,m;
int main()
{
	int i,j;
	int t;
	scanf("%d %d",&n,&m);
	for( i=1;i<=n;i++)
	{
		for( j=1;j<=m;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	
	if(m==1)
	{
		printf("Yes\n");
		return 0;
	} 
	
	if(judge())
	{
		printf("Yes\n");
		return 0;
	}
	
	for(int i1=1;i1<=m;i1++)
	{
		for(int i2=i1+1;i2<=m;i2++)
		{
			//printf("i1=%d i2=%d\n",i1,i2);
			for(int k=1;k<=n;k++)
			{
				int t;
				t        =a[k][i1];
				a[k][i1] =a[k][i2];
				a[k][i2] =t;
			}
			//printf("%d",judge());
			if(judge())
			{
				printf("Yes\n");
				return 0;
			}
			
			for(int k=1;k<=n;k++)
			{
				int t;
				t        =a[k][i1];
				a[k][i1] =a[k][i2];
				a[k][i2] =t;
			}
		}
	} 
	
	printf("No\n");
	return 0;
}


int judge()
{
	int count=0;
	int flag = 0;
	int i,j;
	for( i=1;i<=n;i++)
	{
		count=0;
		for( j=1;j<=m;j++)
		{
			if(a[i][j]!=j)
			{
				count++;
			}
		}
		if(count>2)
		{
			flag =1;
		}
	}
	
	if(flag == 0)
		return 1;
	else 
		return 0; 
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值