CodeForces 724B Batch Sort

本文探讨了一个特定的算法问题,即如何通过有限次元素交换使表格的每一行形成从1到m的有序排列。文章提供了一种解决方案,通过遍历尝试所有可能的列交换组合来检查是否能实现目标状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题意:给出n行m列的数,问能否通过交换使每行的数字排列为1到M,交换股则为,可以任意选择两列交换,并且、
只能交换一次对于每行可以任意选择两个数交换,并且每行只能交换一次或不交换
思路:暴力枚举交换每一列,看行是否满足
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[100][100],n,m;
int judge()
{
	int i,j,flag=0;
	for(i=1;i<=n;i++)
	{
		int 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;
}
int main()
{
	int i,j,k;
	 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(i=1;i<=m;i++)
		{
			for(j=i+1;j<=m;j++)
			{
				for(k=1;k<=n;k++)
				{
					swap(a[k][i],a[k][j]);
				}
				if(judge())
				{
					printf("Yes\n");
					return 0;
				}
				for(k=1;k<=n;k++)
				{
					swap(a[k][i],a[k][j]);
				}
			}
		}
	printf("No\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值