A15235 | Serega the Pirate
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Little pirate Serega robbed a ship with puzzles of different kinds. Among all kinds, he liked only one, the hardest.
A puzzle is a table of $n$ rows and $m$ columns, whose cells contain each number from $1$ to $n \cdot m$ exactly once.
To solve a puzzle, you have to find a sequence of cells in the table, such that any two consecutive cells are adjacent by the side in the table. The sequence can have arbitrary length and should visit each cell one or more times. For a cell containing the number $i$ , denote the position of the first occurrence of this cell in the sequence as $t_i$ . The sequence solves the puzzle, if $t_1 < t_2 < \dots < t_{nm}$ . In other words, the cell with number $x$ should be first visited before the cell with number $x + 1$ for each $x$ .
Let's call a puzzle solvable, if there exists at least one suitable sequence.
In one move Serega can choose two arbitrary cells in the table (not necessarily adjacent by the side) and swap their numbers. He would like to know the minimum number of moves to make his puzzle solvable, but he is too impatient. Thus, please tell if the minimum number of moves is $0$ , $1$ , or at least $2$ . In the case, where $1$ move is required, please also find the number of suitable cell pairs to swap.
A puzzle is a table of $n$ rows and $m$ columns, whose cells contain each number from $1$ to $n \cdot m$ exactly once.
To solve a puzzle, you have to find a sequence of cells in the table, such that any two consecutive cells are adjacent by the side in the table. The sequence can have arbitrary length and should visit each cell one or more times. For a cell containing the number $i$ , denote the position of the first occurrence of this cell in the sequence as $t_i$ . The sequence solves the puzzle, if $t_1 < t_2 < \dots < t_{nm}$ . In other words, the cell with number $x$ should be first visited before the cell with number $x + 1$ for each $x$ .
Let's call a puzzle solvable, if there exists at least one suitable sequence.
In one move Serega can choose two arbitrary cells in the table (not necessarily adjacent by the side) and swap their numbers. He would like to know the minimum number of moves to make his puzzle solvable, but he is too impatient. Thus, please tell if the minimum number of moves is $0$ , $1$ , or at least $2$ . In the case, where $1$ move is required, please also find the number of suitable cell pairs to swap.
输入格式
In the first line there are two whole positive numbers $n, m$ ( $1 \leq n\cdot m \leq 400\,000$ ) — table dimensions.
In the next $n$ lines there are $m$ integer numbers $a_{i1}, a_{i2}, \dots, a_{im}$ ( $1 \le a_{ij} \le nm$ ).
It is guaranteed that every number from $1$ to $nm$ occurs exactly once in the table.
In the next $n$ lines there are $m$ integer numbers $a_{i1}, a_{i2}, \dots, a_{im}$ ( $1 \le a_{ij} \le nm$ ).
It is guaranteed that every number from $1$ to $nm$ occurs exactly once in the table.
输出格式
Let $a$ be the minimum number of moves to make the puzzle solvable.
If $a = 0$ , print $0$ .
If $a = 1$ , print $1$ and the number of valid swaps.
If $a \ge 2$ , print $2$ .
If $a = 0$ , print $0$ .
If $a = 1$ , print $1$ and the number of valid swaps.
If $a \ge 2$ , print $2$ .
输入输出样例
输入 #1
3 3 2 1 3 6 7 4 9 8 5
输出 #1
0
输入 #2
2 3 1 6 4 3 2 5
输出 #2
1 3
输入 #3
1 6 1 6 5 4 3 2
输出 #3
2
In the first example the sequence $(1, 2), (1, 1), (1, 2), (1, 3), (2, 3), (3, 3)$ , $(2, 3), (1, 3), (1, 2), (1, 1), (2, 1), (2, 2), (3, 2), (3, 1)$ solves the puzzle, so the answer is $0$ .
The puzzle in the second example can't be solved, but it's solvable after any of three swaps of cells with values $(1, 5), (1, 6), (2, 6)$ .
The puzzle from the third example requires at least two swaps, so the answer is $2$ .
The puzzle in the second example can't be solved, but it's solvable after any of three swaps of cells with values $(1, 5), (1, 6), (2, 6)$ .
The puzzle from the third example requires at least two swaps, so the answer is $2$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted