A9091 | Three Swaps
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Xenia the horse breeder has $n$ $(n>1)$ horses that stand in a row. Each horse has its own unique number. Initially, the $i$ -th left horse has number $i$ . That is, the sequence of numbers of horses in a row looks as follows (from left to right): 1, 2, 3, $...$ , $n$ .
Xenia trains horses before the performance. During the practice sessions, she consistently gives them commands. Each command is a pair of numbers $l,r$ $(1<=l<r<=n)$ . The command $l,r$ means that the horses that are on the $l$ -th, $(l+1)$ -th, $(l+2)$ -th, $...$ , $r$ -th places from the left must be rearranged. The horses that initially stand on the $l$ -th and $r$ -th places will swap. The horses on the $(l+1)$ -th and $(r-1)$ -th places will swap. The horses on the $(l+2)$ -th and $(r-2)$ -th places will swap and so on. In other words, the horses that were on the segment $[l,r]$ change their order to the reverse one.
For example, if Xenia commanded $l=2,r=5$ , and the sequence of numbers of horses before the command looked as (2, 1, 3, 4, 5, 6), then after the command the sequence will be (2, 5, 4, 3, 1, 6).
We know that during the practice Xenia gave at most three commands of the described form. You have got the final sequence of numbers of horses by the end of the practice. Find what commands Xenia gave during the practice. Note that you do not need to minimize the number of commands in the solution, find any valid sequence of at most three commands.
Xenia trains horses before the performance. During the practice sessions, she consistently gives them commands. Each command is a pair of numbers $l,r$ $(1<=l<r<=n)$ . The command $l,r$ means that the horses that are on the $l$ -th, $(l+1)$ -th, $(l+2)$ -th, $...$ , $r$ -th places from the left must be rearranged. The horses that initially stand on the $l$ -th and $r$ -th places will swap. The horses on the $(l+1)$ -th and $(r-1)$ -th places will swap. The horses on the $(l+2)$ -th and $(r-2)$ -th places will swap and so on. In other words, the horses that were on the segment $[l,r]$ change their order to the reverse one.
For example, if Xenia commanded $l=2,r=5$ , and the sequence of numbers of horses before the command looked as (2, 1, 3, 4, 5, 6), then after the command the sequence will be (2, 5, 4, 3, 1, 6).
We know that during the practice Xenia gave at most three commands of the described form. You have got the final sequence of numbers of horses by the end of the practice. Find what commands Xenia gave during the practice. Note that you do not need to minimize the number of commands in the solution, find any valid sequence of at most three commands.
输入格式
The first line contains an integer $n$ $(2<=n<=1000)$ — the number of horses in the row. The second line contains $n$ distinct integers $a_{1},a_{2},...,a_{n}$ $(1<=a_{i}<=n)$ , where $a_{i}$ is the number of the $i$ -th left horse in the row after the practice.
输出格式
The first line should contain integer $k$ $(0<=k<=3)$ — the number of commads Xenia gave during the practice. In each of the next $k$ lines print two integers. In the $i$ -th line print numbers $l_{i},r_{i}$ $(1<=l_{i}<r_{i}<=n)$ — Xenia's $i$ -th command during the practice.
It is guaranteed that a solution exists. If there are several solutions, you are allowed to print any of them.
It is guaranteed that a solution exists. If there are several solutions, you are allowed to print any of them.
输入输出样例
输入 #1
5 1 4 3 2 5
输出 #1
1 2 4
输入 #2
6 2 1 4 3 6 5
输出 #2
3 1 2 3 4 5 6
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted