Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B

Description

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的形式

解法:暴力,首先从第一行的1,2位置的数交换,交换后看错位的数量是不是小于等于2,大于2那么就算再交换一次也没办法还原了。直到有两个位置符合要求,则把这一次交换看成是列交换,(比如第一组数据)再根据这两个位置,将下一行也交换(因为我们是列交换嘛),判断是否符合小于等于2的条件,直到结束

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[200][200];
int b[200][200];
set<int>q,q2;
map<int,int>p;
map<int,int>::iterator it;
int cmd(int x,int y)
{
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        int num=0;
        swap(b[i][x],b[i][y]);
        for(int j=1;j<=m;j++)
        {
            if(b[i][j]!=j)
            {
                num++;
            }
        }
      //  cout<<x<<" "<<y<<" "<<num<<endl;
        if(num>2)
        {
       //     cout<<"A"<<endl;
            return 0;
        }
    }
    return 1;
}
int main()
{
    int flag=0;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        int num=0;
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
            b[i][j]=a[i][j];
            if(a[i][j]!=j)
            {
                num++;
                p[j]++;
                q.insert(j);
                q2.insert(i);
            }
        }
        if(num>2)
        {
            flag=1;
        }
        // cout<<num<<endl;
    }
    /*  if(q2.size()!=n)
      {
          for(int i=1; i<=n; i++)
          {
              if(p[i]>=3)
              {
                  cout<<"NO"<<endl;
                  return 0;
              }
          }
          cout<<"YES"<<endl;
      }
      else if(q2.size()==n)
      {
          if(q.size()==2)
          {
              cout<<"YES"<<endl;
          }*/
 
    int flag1=0;
    if(flag==0)
    {
        flag1=1;
    }
    for(int i=1;i<=m;i++)
    {
        for(int j=i+1;j<=m;j++)
        {
            if(cmd(i,j))
            {
                cout<<i<<" "<<j<<endl;
                flag1=1;
            }
            for(int _=1;_<=n;_++)
            {
                for(int q=1;q<=m;q++)
                {
                    b[_][q]=a[_][q];
                }
            }
        }
    }
    if(flag1)
    {
        puts("YES");
    }
    else
    {
        puts("NO");
    }
    return 0;
}

  

 

posted @   樱花落舞  阅读(198)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示