【39.77%】【codeforces 724B】Batch Sort
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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).
Examples
input
2 4
1 3 2 4
1 3 4 2
output
YES
input
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
output
NO
input
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
output
YES
Note
In the first sample, one can act in the following way:
Swap second and third columns. Now the table is
1 2 3 4
1 4 3 2
In the second row, swap the second and the fourth elements. Now the table is
1 2 3 4
1 2 3 4
【题解】
先枚举那个交换整列的操作。
然后用这道题的做法判断每一行是不是可以在每一行交换一次元素过后变成有序:
http://blog.csdn.net/harlow_cheng/article/details/52294871
就是找循环节吧。交换
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 25;
struct abc
{
int data;
int key;
};
int n, m;
int a[MAXN][MAXN];
abc b[MAXN];
bool vis[MAXN];
void input(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 +t- '0', t = getchar();
}
bool cmp(abc a, abc b)
{
return a.data < b.data;
}
int get_num(int x)
{
for (int i = 1; i <= m; i++)
b[i].data = a[x][i], b[i].key = i;
sort(b + 1, b + 1 + m, cmp);
for (int i = 1; i <= m; i++)
vis[i] = false;
int num = 0;
for (int i = 1; i <= m; i++)
if (!vis[i])
{
int t = i;
int len = 0;
while (!vis[t])
{
vis[t] = true;
len++;
t = b[t].key;
}
num += len - 1;//len-1就是需要交换的次数
}
return num;
}
bool check()
{
for (int i = 1; i <= n; i++)
{
int num = get_num(i);
if (num > 1)
return false;
}
return true;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input(n); input(m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
input(a[i][j]);
bool flag = false;
flag = check();
if (flag)
{
puts("YES");
return 0;
}
for (int i = 1;i <= m-1;i++)
for (int j = i + 1; j <= m; j++)
{
for (int k = 1; k <= n; k++)
swap(a[k][i], a[k][j]);
flag = check();
if (flag)
{
puts("YES");
return 0;
}
for (int k = 1; k <= n; k++)
swap(a[k][i], a[k][j]);
}
puts("NO");
return 0;
}
【39.77%】【codeforces 724B】Batch Sort的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【39.66%】【codeforces 740C】Alyona and mex
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【23.39%】【codeforces 558C】Amr and Chemistry
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.78%】【codeforces 625C】K-special Tables
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【39.29%】【codeforces 552E】Vanya and Brackets
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 750D】New Year and Fireworks
time limit per test2.5 seconds memory limit per test256 megabytes inputstandard input outputstandard ...
随机推荐
- Android 一个异步SocketHelper
发送流程:首先定义一个缓冲池,发送数据时仅仅是将待发送的数据加入到缓冲池中,再由后台的工作线程从缓冲池中取得待发送数据进行发送.可能某些情况下在数据发送完成时需要做一些处理(比如写日志),便定义了一个 ...
- Css fixed和absolute定位差别
fixed:固定定位 absolute:绝对定位 差别非常easy: 1.没有滚动栏的情况下没有差异 2.在有滚动栏的情况下.fixed定位不会随滚动栏移动而移动.而absolute则会随滚动栏移动 ...
- javascript进阶课程--第三章--匿名函数和闭包
javascript进阶课程--第三章--匿名函数和闭包 一.总结 二.学习要点 掌握匿名函数和闭包的应用 三.匿名函数和闭包 匿名函数 没有函数名字的函数 单独的匿名函数是无法运行和调用的 可以把匿 ...
- mysql三种带事务批量插入
原文:mysql三种带事务批量插入 c#之mysql三种带事务批量插入 前言 对于像我这样的业务程序员开发一些表单内容是家常便饭的事情,说道表单 我们都避免不了多行内容的提交,多行内容保存,自然要用到 ...
- iOS ASIHTTPRequest
ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRe ...
- context.getSystemService的简单说明
在android开发过程中这种方法一定不会陌生,比方我们在获取WindowManager和LayoutInflater对象的时候就须要我们调用这种方法.这种方法在context这个抽象类的一个抽象方法 ...
- HDU 1284 钱币兑换问题 母函数、DP
题目链接:HDU 1284 钱币兑换问题 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- MHA 一主两从搭建-脚本VIP-自动切换
环境介绍:主机名 IP MHA角色 MySQL角色node1 192.168.56.26 Node MySQL Master node2 192.168.56.27 Node MySQL Master ...
- Spring web 工具类 WebApplicationContextUtils
概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...
- [SCSS] Loop Over Data with the SCSS @each Control Directive
The SCSS @for directive is great when we know how many iterations are required and we only need 1 va ...