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.
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.
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).
2 4
1 3 2 4
1 3 4 2
YES
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
NO
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
YES
In the first sample, one can act in the following way:
- Swap second and third columns. Now the table is1 2 3 41 4 3 2
- In the second row, swap the second and the fourth elements. Now the table is1 2 3 41 2 3 4
题意:n*m的矩阵,可以做一行中的两个数交换,或者是列交换(每种最多一次),问可不可以还原成1-m的形式
解法:暴力,首先从第一行的1,2位置的数交换,交换后看错位的数量是不是小于等于2,大于2那么就算再交换一次也没办法还原了。直到有两个位置符合要求,则把这一次交换看成是列交换,(比如第一组数据)再根据这两个位置,将下一行也交换(因为我们是列交换嘛),判断是否符合小于等于2的条件,直到结束
#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;
}
Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B的更多相关文章
- 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)D Dense Subsequence
传送门:D Dense Subsequence 题意:输入一个m,然后输入一个字符串,从字符串中取出一些字符组成一个串,要求满足:在任意长度为m的区间内都至少有一个字符被取到,找出所有可能性中字典序最 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort
链接 题意:输入n,m,表示一个n行m列的矩阵,每一行数字都是1-m,顺序可能是乱的,每一行可以交换任意2个数的位置,并且可以交换任意2列的所有数 问是否可以使每一行严格递增 思路:暴力枚举所有可能的 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing
我不告诉你这个链接是什么 分析:模拟可以过,但是好烦啊..不会写.还有一个扩展欧几里得的方法,见下: 假设光线没有反射,而是对应的感应器镜面对称了一下的话 左下角红色的地方是原始的的方格,剩下的三个格 ...
- 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 ...
- 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 ...
- 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 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A
Description You are given names of two days of the week. Please, determine whether it is possible th ...
随机推荐
- VCL Tclientsocket, Tserversocket控件安装方法
菜单component->Install Packets 按Add按钮,选择delphi目录里的bin目录下的dclsockets70.bpl(delphi2010是dclsockets140. ...
- iis access denied, you do not have permission.
this kind of problems are usually caused by some IIS configuration issues, like application pool set ...
- [原创]java WEB学习笔记52:国际化 fmt 标签,国际化的总结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- My sql 日期格式化
DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. 语法 DATE_FORMAT(date,format) select DATE_FORMAT(NOW(),'%Y-%m-%d %H ...
- NOIP201302表达式求值
NOIP201302表达式求值 题目描述 Description 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入描述 Input Description 输入仅有一行,为需要你计 ...
- Oracle DB SQL 性能分析器
• 确定使用SQL 性能分析器的优点 • 描述SQL 性能分析器工作流阶段 • 使用SQL 性能分析器确定数据库更改所带来的性能改进 SQL 性能分析器:概览 • 11g 的新增功能 • 目标用户:D ...
- android 开发(百度地图)
百度地图开放平台:http://lbsyun.baidu.com/ 百度地图API:http://developer.baidu.com/map/reference/ Android地图SDK 百度地 ...
- nodejs和mongodb实践
首先,当然是都安装了nodejs 和mongodb了.这必须是前提条件. 现在我们要用nodejs连接mongodb数据库了.我这里只是一个非常非常简单是实践,初学嘛.更深入的学习之后,我会仔细写笔记 ...
- AC68U 内Linux 终端前后的切换,终端挂起和恢复
ssh 登录终端后, 如果想切换到本地上来, 可以按: -,Ctrl+Z 如果要恢复到远程端,则命令: fg
- msyql数据库位置
MySQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 cd /home mkdir data 2 ...