CF91D.Grocer's Problem
省选/NOI-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Yesterday was a fair in a supermarket's grocery section. There were n jars with spices on the fair. Before the event the jars were numbered from 1 to n from the left to the right. After the event the jars were moved and the grocer had to sort them by the increasing of the numbers.
The grocer has a special machine at his disposal. The machine can take any 5 or less jars and rearrange them in the way the grocer wants. Note that the jars do not have to stand consecutively. For example, from the permutation 2, 6, 5, 4, 3, 1 one can get permutation 1, 2, 3, 4, 5, 6, if pick the jars on the positions 1, 2, 3, 5 and 6.
Which minimum number of such operations is needed to arrange all the jars in the order of their numbers' increasing?
昨天,超市的食品区举办了一场展销会。展销会上共有 n 个装有香料的罐子。活动开始前,这些罐子从左到右依次编号为 1 到 n。活动结束后,罐子的位置被打乱,售货员需要将它们按编号升序重新排列。
售货员拥有一台特殊的机器可供使用。该机器每次最多可选取任意 5 个罐子(不要求位置连续),并按售货员指定的顺序重新排列它们。例如,对于排列 2,6,5,4,3,1,若选取位于第 1,2,3,5,6 位上的罐子(即数值为 2,6,5,3,1 的罐子),则可将其重排为 1,2,3,4,5,6。
问:将所有罐子按编号升序排列所需的最少操作次数是多少?
输入格式
The first line contains an integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a__i (1 ≤ a__i ≤ n) — the i-th number represents the number of a jar that occupies the i-th position. It is guaranteed that all the numbers are distinct.
第一行包含一个整数 n(1≤n≤105)。第二行包含 n 个用空格分隔的整数 ai(1≤ai≤n)——其中第 i 个数表示占据第 i 个位置的罐子的编号。保证所有数字互不相同。
输出格式
Print on the first line the least number of operations needed to rearrange all the jars in the order of the numbers' increasing. Then print the description of all actions in the following format.
On the first line of the description of one action indicate the number of jars that need to be taken (k), on the second line indicate from which positions the jars need to be taken (_b_1, _b_2, ..., b__k), on the third line indicate the jar's new order (_c_1, _c_2, ..., c__k). After the operation is fulfilled the jar from position b__i will occupy the position c__i. The set (_c_1, _c_2, ..., c__k) should be the rearrangement of the set (_b_1, _b_2, ..., b__k).
If there are multiple solutions, output any.
第一行输出将所有罐子按数字升序重新排列所需的最少操作次数。然后,按以下格式输出所有操作的描述。
每个操作的描述包含三行:
第一行输出需要移动的罐子数量 k;
第二行输出需要移动的罐子所在的位置 b1, b2, …, bk;
第三行输出这些罐子移动后的新位置顺序 c1, c2, …, ck。执行该操作后,位于位置 bi 的罐子将移动至位置 ci。集合 (c1, c2, …, ck) 必须是集合 (b1, b2, …, bk) 的一个排列。
若存在多种解法,输出任意一种即可。
输入输出样例
输入#1
6 3 5 6 1 2 4
输出#1
2 4 1 3 6 4 3 6 4 1 2 2 5 5 2
输入#2
14 9 13 11 3 10 7 12 14 1 5 4 6 8 2
输出#2
3 4 2 13 8 14 13 8 14 2 5 6 7 12 5 10 7 12 6 10 5 5 3 11 4 1 9 11 4 3 9 1
说明/提示
Let's consider the first sample. The jars can be sorted within two actions.
During the first action we take the jars from positions 1, 3, 6 and 4 and put them so that the jar that used to occupy the position 1 will occupy the position 3 after the operation is completed. The jar from position 3 will end up in position 6, the jar from position 6 will end up in position 4 and the jar from position 4 will end up in position 1.
After the first action the order will look like that: 1, 5, 3, 4, 2, 6.
During the second operation the jars in positions 2 and 5 will change places.
我们来考虑第一个样例。这些罐子可以在两次操作内完成排序。
在第一次操作中,我们选取位置为 1、3、6 和 4 的罐子,并进行如下置换:原来位于位置 1 的罐子在操作完成后将位于位置 3;原来位于位置 3 的罐子将位于位置 6;原来位于位置 6 的罐子将位于位置 4;原来位于位置 4 的罐子将位于位置 1。
第一次操作后,序列变为:1, 5, 3, 4, 2, 6。
在第二次操作中,位置 2 和位置 5 上的罐子将互换位置。
输入解题思路,AI测评打分。不知道怎么写?