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 41 3 2 41 3 4 2
4 41 2 3 42 3 4 13 4 1 24 1 2 3
3 62 1 3 4 5 61 2 4 3 5 61 2 3 4 6 5
Sample Output
YES NO YES
思路
题意:给你一个NxM的矩阵块,每行都是数字1-M的一个排列,对于行,每行只允许交换两个数的位置一次,对于列,只允许交换两列一次,最多可交换次数为N + 1次,问有没有可能在允许的交换操作下使得每行都是上升序列。
数据范围很小,考虑暴力解。首先判断是否能只通过行操作,达到目的,不行的话,枚举列交换,然后在判断能否通过行操作达到目的。
#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;
}
Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)的更多相关文章
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...
- 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 Y ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)
题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...
- 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D
http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划
E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D. Dense Subsequence 暴力
D. Dense Subsequence 题目连接: http://codeforces.com/contest/724/problem/D Description You are given a s ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 数学
C. Ray Tracing 题目连接: http://codeforces.com/contest/724/problem/C Description oThere are k sensors lo ...
随机推荐
- C# 7.0 新特性1: 基于Tuple的“多”返回值方法
本文基于Roslyn项目中的Issue:#347 展开讨论. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: ...
- list转datatable
/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...
- 基于FPGA的音频信号的FIR滤波(Matlab+Modelsim验证)
1 设计内容 本设计是基于FPGA的音频信号FIR低通滤波,根据要求,采用Matlab对WAV音频文件进行读取和添加噪声信号.FFT分析.FIR滤波处理,并分析滤波的效果.通过Matlab的分析验证滤 ...
- linux 防火墙开放特定端口与指定ip谨防
vi etc/iptable/sysconfig/iptables linux 开放固定端口 -A INPUT -m state --state NEW -m tcp -p tcp --dport 1 ...
- Linux 配置只安装 64 位软件包
Centos.RHEL等系统,yum 安装的时候有时候会安装32的,然而我们只需要安装64位的软件! 解决方法: yum只安装 64 位的包,只需在 /etc/yum.conf 中加入 "e ...
- Putty SSH简单使用
本地的puttygen生出的秘钥,公钥传到服务器上连接会报错 Server refused our key. 一般我们建议都在服务器上生成秘钥,把私钥下载下来.加载到putty认证中 01.在服务器上 ...
- Android Intent应用
1. 显示Intent // 直接设置Content和到下一个的Actvity的名字 Intent i = new Intent(MainActivity.this, AnotherAty.class ...
- jQ1.5中的事件系统(低版本的事件系统)
jQ的一个个版本事系统都在修正着bug和不断优化, 而且看了事件系统对事件的兼容更加熟悉, 更加了解jQ内部的事件机制. 因为jQ对事件系统引入了事件命名空间,事件的代理, 事件的手动触发,事件描述等 ...
- eclipse-debug时直接进入/不进入/提示进入调试页面修改
eclipse使用debug调试程序时 默认设置每次程序走到断点位置时提示是否进入调试页面(如图) 而个人习惯有些系统直接进入调试页面.也有些人系统不进入调试页面调试 在这里勾选Remember my ...
- selenium3各种报错解决办法
解决办法全在这个链接里 http://learn-automation.com/use-firefox-selenium-using-geckodriver-selenium-3