【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 ...
随机推荐
- Altium Designer中原理图和pcb交叉查找
转自:https://wenku.baidu.com/view/53ca06badbef5ef7ba0d4a7302768e9951e76e33.html 再看看:::: 转自:http://blog ...
- 过滤input框中的特殊字符
两种方式,我觉得是一样的效果,请看: var charFilter1 = function(str) { var pattern = new RegExp("[`~!@#$^&*() ...
- 8.1 Android灯光系统_总体框架
1.框架 APP(java语言实现) ------------------------------- JNI(c++语言实现) 向上提供Java执行c函数的接口 向下访问HAL ------ ...
- 【BZOJ 4556】字符串
[链接]h在这里写链接 [题意] 给你一个长度为n(n<=10^5)的字符串以及一个整数m(m<=10^5),代表询问的次数. 每个询问由4个整数a,b,c,d给出 ...
- [WASM] Create and Run a Native WebAssembly Function
In this introduction, we show a simple WebAssembly function that returns the square root of a number ...
- percona-toolkit源码编译安装
安装依赖软件yum install perl-ExtUtils-CBuilder perl-ExtUtils-MakeMakeryum install perl-Time-HiRes perl-DB ...
- 魔兽争霸war3心得体会(一):UD的冰甲蜘蛛流
玩war3好几年了,之前都是打打电脑,随便玩玩的.刚刚在浩方等平台上和人玩的时候,各种被虐,很难赢一局.从去年开始,才认真玩.思考下各种战术. 最初,使用的是兽族orc,后来觉得兽族不够厉害,玩到对战 ...
- CSS文本阴影实例
原文 简书原文:https://www.jianshu.com/p/5abf2fa2f1b9 前言 以下的实例是我从<CSS实战>中看到的实例,当我看到这些实例的时候,发现平时不是很在意的 ...
- WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo
WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo 前言 12年前我入行三天.用table布局做了一个非常粗糙的网页.我说了一句话,"网页就是表格加文字加图片,图片分两种 ...
- ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型
一:KVC和KVO的学习 #import "StatusItem.h" /* 1:总结:KVC赋值:1:setValuesForKeysWithDictionary实现原理:遍历字 ...