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 ...
随机推荐
- poj 1475 || zoj 249 Pushing Boxes
http://poj.org/problem?id=1475 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=249 Pushin ...
- Hibernate二进制或大文件类型数据和Oracle交互
//测试存储二进制文件 @Test public void test() throws IOException{ InputStream in=new FileInputStream("E ...
- bzoj3192 [JLOI2013]删除物品
用数组表示两个栈,将两个栈的栈顶并在一起,用树状数组维护一下操作即可. 代码 #include<cstdio> #include<algorithm> #include< ...
- Spring 中的 Bean 配置
内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...
- PHP V5.2 中的新增功能,第 1 部分: 使用新的内存管理器
PHP V5.2:开始 2006 年 11 月发布了 PHP V5.2,它包括许多新增功能和错误修正.它废止了 5.1 版并被推荐给所有 PHP V5 用户进行升级.我最喜欢的实验室环境 —— Win ...
- 【NOIP模拟赛】lover——心上人
心上人 [问题描述] 人到初三,总会遇到情感问题,比方说小 J 就喜欢上了小 W.于是小 J 就需要说一长串的话讨小 W 欢心.现在已知小 W 听到一些词就会很高兴,而且白听不厌,但她又讨厌小 J ...
- java小程序整理及排序算法
1. 利用循环打印如下图形 ***** **** *** ** * public class Main { public static void main(String[] args) { // TO ...
- <c:if>条件判断 和 取值做乘法运算
http://www.yiibai.com/jstl/jstl_core_if_tag.html <td> ${log.etdhHandleTime }<span> < ...
- win32 treeview
// 1.create treeview DWORD dwStryle = WS_VISIBLE | WS_CHILD | TVS_HASLINES|TVS_SHOWSELALWAYS/*|TVS_L ...
- laravel5.0升级到laravel5.1
1.修改composer.json.将其中的"laravel/framework": "5.0.*"修改为"laravel/framework&quo ...