编程介的小学生 2017-07-28 06:27 采纳率: 20.5%
浏览 721
已采纳

Circular Lamps

Problem Description
The circular roundabout of 2nd road is an important traffic junction in Nanjing University of Science and Technology. To celebrate the 60th anniversary, the school officer decides to build some lamps with digital shape surrounding the circular roundabout.
There are 2N lamps to build. Lamps have the shape of digits, ranging from 1 to 2N. Specially, lamps will be built as a whole which can’t be split into single numbers. For example, lamp 19 can’t be split into lamp 1 and lamp 9. Since the roundabout is circular, the last lamp is next to the first lamp. Unfortunately, due to the carelessness of the construction team, some lamps are placed in the wrong position. Since the construction team is now
absent after finishing their work, we could only correct the order of lamps by ourselves. After rearrangement, the lamp with the shape i (1 <= i <= 2N) is expected to be placed on the i-th position. In other words, the final permutation should be 1, 2, ... , 2N. Because of the large volume of the lamps, we have only two ways to adjust the order

1) Reverse the consecutive 4 lamps starting from the position x, denotes as (1 x). For example:
(1 4): 1 2 3 [4 5 6 7] 8 -> 1 2 3 [7 6 5 4] 8
or
(1 6): 1] 2 3 4 5 [6 7 8 -> 6] 2 3 4 5 [1 8 7
where ‘[’ stands for the start position, and ‘]’ stands for the end position respectively.
2) Shift all lamps to the left by x positions, denotes as (2 x). For example:
(2 4): 1 2 3 4 5 6 7 8 -> 5 6 7 8 1 2 3 4
or
(2 7): 1 2 3 4 5 6 7 8 -> 8 1 2 3 4 5 6 7

The data guarantees the existence of a valid adjustment sequence. Also the case where all lamps are already placed correctly will not exist in the input.

Your task is to generate a valid adjustment sequence that all lamps are put on the correct position.

Input
There are several test cases, please process till EOF.
Each test case starts with a line containing one integers N(2 <= N <= 30). The next line contains 2N different integers, the i-th number Ai denotes that the shape of the i-th lamp is Ai(1 <= Ai <= 2N).

Output
For each test case, the first line of the output should contain a single integer S <= 400000, denoting the number of swap operations you need to perform.
Each of the following S lines should consist of two integers. The first one may be 1 or 2, indicating the type of swap operations. For the first type, the second number is the start position of the operation, and for the second type, the second number is the positions you need to shift. In both cases, the second number should be ranged between 1 and 2N.
If there are multiple possible solutions satisfying the conditions above, any one will be accepted. Please don’t print extra empty lines, spaces and other irrelevant characters.

Sample Input
4
6 5 4 3 2 1 8 7
4
4 5 1 2 3 6 7 8
4
7 8 4 3 2 1 5 6

Sample Output
2
1 2
1 6
2
1 2
1 1
2
1 3
2 2

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-08-13 15:51
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题