zoukankan      html  css  js  c++  java
  • 【39.77%】【codeforces 724B】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

    【题解】

    先枚举那个交换整列的操作。
    然后用这道题的做法判断每一行是不是可以在每一行交换一次元素过后变成有序:
    http://blog.csdn.net/harlow_cheng/article/details/52294871
    就是找循环节吧。交换

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    const int MAXN = 25;
    
    struct abc
    {
        int data;
        int key;
    };
    
    int n, m;
    int a[MAXN][MAXN];
    abc b[MAXN];
    bool vis[MAXN];
    
    void input(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 +t- '0', t = getchar();
    }
    
    bool cmp(abc a, abc b)
    {
        return a.data < b.data;
    }
    
    int get_num(int x)
    {
        for (int i = 1; i <= m; i++)
            b[i].data = a[x][i], b[i].key = i;
        sort(b + 1, b + 1 + m, cmp);
        for (int i = 1; i <= m; i++)
            vis[i] = false;
        int num = 0;
        for (int i = 1; i <= m; i++)
            if (!vis[i])
            {
                int t = i;
                int len = 0;
                while (!vis[t])
                {
                    vis[t] = true;
                    len++;
                    t = b[t].key;
                }
                num += len - 1;//len-1就是需要交换的次数
            }
        return num;
    }
    
    bool check()
    {
        for (int i = 1; i <= n; i++)
        {
            int num = get_num(i);
            if (num > 1)
                return false;
        }
        return true;
    }
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        input(n); input(m);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                input(a[i][j]);
        bool flag = false;
        flag = check();
        if (flag)
        {
            puts("YES");
            return 0;
        }
        for (int i = 1;i <= m-1;i++)
            for (int j = i + 1; j <= m; j++)
            {
                for (int k = 1; k <= n; k++)
                    swap(a[k][i], a[k][j]);
                flag = check();
                if (flag)
                {
                    puts("YES");
                    return 0;
                }
                for (int k = 1; k <= n; k++)
                    swap(a[k][i], a[k][j]);
            }
        puts("NO");
        return 0;
    }
  • 相关阅读:
    KubeCon 2020 演讲集锦|《阿里巴巴云原生技术与实践 13 讲》开放下载
    突围数字化转型,让特步同比增长24.8%的全渠道中台
    阿里云飞天大数据产品价值解读——《一站式高质量搜索开放搜索》
    高德AR驾车导航解决方案
    我在阿里写代码学会的六件事
    送外卖也要“黑科技”?阿里移动感知技术应用揭秘
    阿里云机器学习怎么玩?这本新手入门指南揭秘了!
    用户自定义类型03 零基础入门学习Delphi33
    用户自定义类型03 零基础入门学习Delphi33
    用户自定义类型01 零基础入门学习Delphi31
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632166.html
Copyright © 2011-2022 走看看