A1216 | [COCI-2010_2011-olympiad]#4 SORT
来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0
题目描述
In order to automate his workload at the factory, Mirko wants to put up to use his old box-sorting robot. There are N boxes in the factory, and each box is labelled with an unique integer in range 1 to N. Mirko's task is to sort the boxes in the ascending order of their labels.
Sorting robot can only perform one specific operation: given the sequence of positions, robot can do a cyclic swap of boxes at those positions. Given sequence does not contain any position more than once.
For example, let's assume that the boxes are currently in order [4, 1, 5, 2, 3], and Mirko provides his robot with the sequence [2, 1, 3]. Robot will then rearrange the boxes so that the second box will go to position 1, first box will go to the position 3, and the third one will take the position
2. Obtained sequence of labels is [1, 5, 4, 2, 3].
Write a program that will sort the boxes using the minimal number of operations. Each sequence given to the robot can be arbitrary long.
Sorting robot can only perform one specific operation: given the sequence of positions, robot can do a cyclic swap of boxes at those positions. Given sequence does not contain any position more than once.
For example, let's assume that the boxes are currently in order [4, 1, 5, 2, 3], and Mirko provides his robot with the sequence [2, 1, 3]. Robot will then rearrange the boxes so that the second box will go to position 1, first box will go to the position 3, and the third one will take the position
2. Obtained sequence of labels is [1, 5, 4, 2, 3].
Write a program that will sort the boxes using the minimal number of operations. Each sequence given to the robot can be arbitrary long.
输入格式
The first line of input contains integer N (2 ≤ N ≤ 1000), the number of boxes in the factory.
The following line contains N integers in range 1 to N, labels of boxes in order. No integer appears twice.
The following line contains N integers in range 1 to N, labels of boxes in order. No integer appears twice.
输出格式
The first line should contain the integer X, minimum number of operations required.
The following X lines should contain the sequences given to robot, one sequence per line.
Each line should start with the length of the sequence, followed by a colon, a single white space, and then space seperated sequence of positions.
NOTE: There may be multiple solutions and you can output any of them.
The following X lines should contain the sequences given to robot, one sequence per line.
Each line should start with the length of the sequence, followed by a colon, a single white space, and then space seperated sequence of positions.
NOTE: There may be multiple solutions and you can output any of them.
输入输出样例
输入 #1
3 3 2 1
输出 #1
1 2: 3 1
输入 #2
5 2 3 1 5 4
输出 #2
2 3: 1 2 3 2: 5 4
输入 #3
5 1 2 3 4 5
输出 #3
0
Program will receive 50% of the points assigned to that test case if the number of operations is not
minimal but is not greater than 1000. Of course, operations provided should yield an sorted sequence.
minimal but is not greater than 1000. Of course, operations provided should yield an sorted sequence.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted