zoukankan      html  css  js  c++  java
  • Batch Sort

    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
    分析:暴力交换列,看每行是否满足;
    代码:
    #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++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    const int maxn=1e5+10;
    using namespace std;
    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;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,c[21][21],vis[21][21],num[4];
    struct node
    {
        int cnt,id;
        bool operator<(const node&p)const
        {
            return cnt>p.cnt;
        }
    }ca[25];
    bool flag;
    bool solve(int a,int b)
    {
        int d[21][21];
        int i,j;
        rep(i,1,n)rep(j,1,m)d[i][j]=c[i][j];
        rep(i,1,n)swap(d[i][a],d[i][b]);
        rep(i,1,n)
        {
            int now=0;
            rep(j,1,m)if(d[i][j]!=j)now++;
            if(now>2)return false;
        }
        return true;
    }
    int main()
    {
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)ca[i].id=i;
        rep(i,1,n)rep(j,1,m)
        {
            scanf("%d",&c[i][j]);
            if(c[i][j]!=j)ca[i].cnt++;
        }
        sort(ca+1,ca+n+1);
        if(ca[1].cnt<=2)puts("YES");
        else if(ca[1].cnt>4)puts("NO");
        else
        {
            j=0;
            int pos=ca[1].id;
            rep(i,1,m)if(c[pos][i]!=i)num[j++]=i;
            rep(i,0,3)rep(j,i+1,3)
            {
                if(solve(num[i],num[j]))return 0*puts("YES");
            }
            puts("NO");
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    关于获取系统平台下的根目录名,分隔符等等
    AlineTransform 的应用
    网上书店例子(JSP和JavaBean)
    在Servlet中实现永久计数器思想
    关于TOmcat的一些小小的知识
    Java 编程基础,应用与实例
    日历类显示多一行
    使用UPnP连接远程交易服务(翻译)
    涂鸦板代码
    网站
  • 原文地址:https://www.cnblogs.com/dyzll/p/5941625.html
Copyright © 2011-2022 走看看