zoukankan      html  css  js  c++  java
  • 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

    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 4
    1 3 2 4
    1 3 4 2

    Sample Output

    YES

    Hint

    题意

    给你n行,每行都是一个1-m的排列。

    你可以交换任意两列,并且你可以每行最多交换两个元素,问你能不能使得每行都是单增的

    题解:

    暴力枚举嘛,数据范围这么小

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 55;
    
    int a[maxn][maxn],n,m;
    bool check(int x,int y)
    {
        for(int i=1;i<=n;i++)
        {
            int flag=0;
            for(int j=1;j<=m;j++)
            {
                int tmp=a[i][j];
                if(j==x)tmp=a[i][y];
                if(j==y)tmp=a[i][x];
                if(tmp!=j)flag++;
            }
            if(flag!=0&&flag!=2)return false;
        }
        return true;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        for(int i=1;i<=m;i++)
            for(int j=1;j<=i;j++)
                if(check(i,j))
                    return puts("YES"),0;
        printf("NO");
    }
  • 相关阅读:
    java swing学习
    JCheckBox相关知识点
    【python 第五日】 函数闭包与装饰器
    【python第四日】 文件处理 生成器 迭代器
    【Python3 第三日】%和format格式化输出 函数
    【python第二日】运算符 数据类型(数字 字符串 列表 元组 字典 集合) 重新定义比较大小
    怎么设置博客园样式
    【python】第一日 python2和python3区别 命名方式 三种结构
    mybatis-generator.xml
    SpringBoot集成mybatis和mybatis generator
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5941803.html
Copyright © 2011-2022 走看看