COI '10 #1 Sort

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type

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 a unique integer in the range 1 to N. Mirko's task is to sort the boxes in the ascending order of their labels.

The sorting robot can only perform one specific operation: given the sequence of positions, the robot can do a cyclic swap of boxes at those positions. The 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]. The robot will then rearrange the boxes so that the second box will go to position 1, the first box will go to position 3, and the third one will take 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.

Input Specification

The first line of input contains integer N (2 \le N \le 1\,000), the number of boxes in the factory.

The following line contains N integers in the range 1 to N, labels of boxes in order. No integer appears twice.

Output Specification

The first line should contain the integer X, the minimum number of operations required.

The following X lines should contain the sequences given to the 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 separated sequence of positions.

NOTE: There may be multiple solutions and you can output any of them.

Scoring

You will receive 50\% of the points assigned to that test case if the number of operations is not minimal but is not greater than 1\,000. Of course, the operations provided should yield a sorted sequence.

Sample Input 1

3
3 2 1

Sample Output 1

1
2: 3 1

Sample Input 2

5
2 3 1 5 4

Sample Output 2

2
3: 1 2 3
2: 5 4

Sample Input 3

5
1 2 3 4 5

Sample Output 3

0

Comments

There are no comments at the moment.