【codeforces 724B Batch Sort】+ 枚举

B. Batch Sort
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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:

Swap second and third columns. Now the table is
1 2 3 4
1 4 3 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的一个递增序列。保证输入的矩阵每一行的数据都是从1-m的。

思路:

因为将两列的元素交换这样的操作只有一次,那么我们首先暴力枚举出来两列,使得这两列元素进行交换之后,我们再进行每一行的判断。

上周的CF B题,居然今天才补上,果然还是菜的不行

#include<cstdio>
#include<algorithm>
using namespace std;
int pa[22][22],N,M;
bool solve(int a,int b)
{
    int i,j,pl;
    for(i = 1 ; i <= N ; i++) swap(pa[i][a],pa[i][b]);
    for(i = 1 ; i <= N; i++){
        pl = 0;
        for(j = 1 ; j <= M ; j++)
            if(pa[i][j] != j)
                pl++;
        if(pl > 2){
            for(i = 1 ; i <= N ; i++) swap(pa[i][a],pa[i][b]);
            return false;
        }
    }
    return true;
}
int main()
{
    int i,j;
    scanf("%d%d",&N,&M);
    for(i = 1 ; i <= N; i++)
        for(j = 1 ; j <= M ; j++)
            scanf("%d",&pa[i][j]);
    for(i = 1 ; i <= M ; i++)
        for(j = i ; j <= M ; j++)
            if(solve(i,j)){
                printf("YES\n");
                return 0;
            }
    printf("NO\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值