[CF724B]Batch Sort(暴力,思维)
题目链接:http://codeforces.com/contest/724/problem/B
题意:给出n*m的数字阵,每行数都是1-m的全排列,最多可以交换2个数一次,整个矩阵可以交换两列一次。问在n+1次操作内是否可以让这整个矩阵每行都变成单调递增的。
先考虑不用交换列的情况,那么只需要关心每行的数字是否在自己的位置上。记下不在自己的位置上的数字个数,如果大于2则说明至少要交换2次。显然是不符合条件的。
接下来考虑交换列的情况,枚举所有列交换的可能,再看看交换过后是否符合每行只需要交换两个数就行了。
#include <bits/stdc++.h> using namespace std; const int maxn = ;
int G[maxn][maxn];
int n, m; bool ok(int G[maxn][maxn]) {
for(int i = ; i <= n; i++) {
int cnt = ;
for(int j = ; j <= m; j++) {
if(G[i][j] != j) cnt++;
}
if(cnt > ) return ;
}
return ;
} int main() {
// freopen("in", "r", stdin);
while(~scanf("%d%d",&n,&m)) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= m; j++) {
scanf("%d", &G[i][j]);
}
}
if(ok(G)) {
puts("YES");
continue;
}
bool flag = ;
for(int i = ; i <= m; i++) {
if(flag) break;
for(int j = i+; j <= m; j++) {
if(flag) break;
for(int k = ; k <= n; k++) swap(G[k][i], G[k][j]);
if(ok(G)) flag = ;
for(int k = ; k <= n; k++) swap(G[k][i], G[k][j]);
}
}
printf("%s\n", flag?"YES":"NO");
}
return ;
}
[CF724B]Batch Sort(暴力,思维)的更多相关文章
- 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 ...
- CF724B. Batch Sort[枚举]
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- codeforces 724B Batch Sort(暴力-列交换一次每行交换一次)
题目链接:http://codeforces.com/problemset/problem/724/B 题目大意: 给出N*M矩阵,对于该矩阵有两种操作: (保证,每行输入的数是 1-m 之间的数且不 ...
- CodeForces 742B Batch Sort
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Batch Sort
Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- 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 ...
- 1250 Super Fast Fourier Transform(湘潭邀请赛 暴力 思维)
湘潭邀请赛的一题,名字叫"超级FFT"最终暴力就行,还是思维不够灵活,要吸取教训. 由于每组数据总量只有1e5这个级别,和不超过1e6,故先预处理再暴力即可. #include&l ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort
链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...
- codeforces724-B. Batch Sort
想着想着就忘了有什么问题没解决,坑啊 一开始读错题意了,而且一着急写了两大段差不多的代码,冗余度啊,不说了.. 显然的一点,给的数据是绝对离散的,每行都是1~m的排列 难点一.如何移动能使未排序的数组 ...
随机推荐
- ubuntu使用记录
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir ...
- CentOS安装、卸载jdk
安装:http://www.mamicode.com/info-detail-613410.html 卸载:http://sunqiusong.email.blog.163.com/blog/stat ...
- prop
用法:prop(属性|key,value|fn) 用例:点击全选/取消全选 // 全选 和全不选 $("#check_all").click(function () { if ($ ...
- YTU 2296: KMP模式匹配 二(串)
2296: KMP模式匹配 二(串) 时间限制: 1 Sec 内存限制: 128 MB 提交: 29 解决: 17 题目描述 输入一个主串和一个子串,用KMP进行匹配,问进行几趟匹配才成功,若没成 ...
- c#作业
string a = this.textBox1.Text; // string [] b=a.Split("\r\n".ToCharArray(),StringSplitOpti ...
- Java内部类、静态嵌套类、局部内部类、匿名内部类
Nested classes are further divided into two types: static nested classes: If the nested class is sta ...
- ThinkPHP使用PHPmailer发送Email邮件
下面介绍thinkphp如何使用phpmailer发送邮件,使用这个邮件发送类,配置好参数后,一句话即可发送邮件.仅适合于thinkphp框架. 第一步,下载类库 将Mail.class.php复制到 ...
- Ramsey theorem in Combinarotics
- JQuery的$(document).ready(function(){})与JS的window.onload 的各自优势!
由于项目需要,使用JQuery也有相当一段时间了.由于经常要处理DOM节点加载.图片显示以及动态资源请求,所以对$(document).ready(function(){})理解也越来越深了,所有在此 ...
- Semi-prime H-numbers(筛法)
Semi-prime H-numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8069 Accepted: 3479 ...