Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)
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).
Sample Input
2 4
1 3 2 4
1 3 4 2
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
Sample Output
YES
NO
YES
思路
题意:给你一个NxM的矩阵块,每行都是数字1-M的一个排列,对于行,每行只允许交换两个数的位置一次,对于列,只允许交换两列一次,最多可交换次数为N + 1次,问有没有可能在允许的交换操作下使得每行都是上升序列。
数据范围很小,考虑暴力解。首先判断是否能只通过行操作,达到目的,不行的话,枚举列交换,然后在判断能否通过行操作达到目的。
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 | #include<bits/stdc++.h> using namespace std; const int maxn = 25; bool solve( int a[][maxn], int N, int M, int c1, int c2) { int atmp[maxn][maxn]; for ( int i = 0;i < N;i++) for ( int j = 0;j < M;j++) atmp[i][j] = a[i][j]; for ( int i = 0;i < N;i++) swap(atmp[i][c1],atmp[i][c2]); for ( int i = 0;i < N;i++) { int cnt = 0; for ( int j = 0;j < M;j++) { if (atmp[i][j] != j + 1) cnt++; } if (cnt > 2) return false ; } return true ; } int main() { int N,M,a[maxn][maxn]; while (~ scanf ( "%d%d" ,&N,&M)) { for ( int i = 0;i < N;i++) for ( int j = 0;j < M;j++) scanf ( "%d" ,&a[i][j]); bool flag = false ; for ( int i = 0;i < M - 1;i++) { for ( int j = i + 1;j < M;j++) { flag = solve(a,N,M,i,j); if (flag) break ; } if (flag) break ; } if (flag) printf ( "YES\n" ); else { int cnt = 0; for ( int i = 0;i < N;i++) { cnt = 0; for ( int j = 0;j < M;j++) { if (a[i][j] != j + 1) cnt++; if (cnt > 2) break ; } if (cnt > 2) break ; } if (cnt > 2) printf ( "NO\n" ); else printf ( "YES\n" ); } } return 0; } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)