33OJ
登录 注册

#P17825. [COCI 2026/2027 #1] 三堆箱子 / Tri

    ID: 20479 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>递归Special Judge分治排序构造COCI(克罗地亚)2026

[COCI 2026/2027 #1] 三堆箱子 / Tri

Problem Description

Ana works in a warehouse that has three piles of boxes, labelled 11, 22 and 33. Each box has a number from 11 to nn written on it, and every number appears on exactly one box.

At the beginning, all the boxes are stacked on top of each other on pile 11, in some order, while piles 22 and 33 are empty.

The boxes are heavy, so Ana moves them one at a time. In one move, she may take the box from the top of one pile and put it on top of a different pile. She must not touch a box that is not on top of its pile, or the pile would fall over.

Ana wants all the boxes to end up on pile 11, stacked in increasing order from bottom to top: the box with number 11 at the bottom, the box with number 22 on top of it, and so on, up to the box with number nn at the top. Piles 22 and 33 must be empty at the end.

Ana is short on time, so she may make at most MM moves. Help her find a sequence of moves that achieves this arrangement. The number of moves does not have to be the smallest possible, but it must not be greater than MM.

Input Format

The first line contains positive integers nn and MM (1≤n≤5001 \le n \le 500, 1000≤M≤260 0001000 \le M \le 260\,000), the number of boxes and the maximum allowed number of moves.

The second line contains nn distinct positive integers p1,p2,…,pnp_1,p_2,\ldots,p_n (1≤pi≤n1 \le p_i \le n), the numbers written on the boxes on pile 11, listed from bottom to top. In other words, p1p_1 is the number on the lowest box, and pnp_n is the number on the box at the top of the pile.

Output Format

In the first line, output an integer KK (0≤K≤M0 \le K \le M), the number of moves Ana should make.

In the ii-th of the following KK lines, output two different numbers aia_i and bib_i (ai,bi∈{1,2,3}a_i,b_i \in \{1,2,3\}) describing the ii-th move: Ana takes the box from the top of pile aia_i and puts it on top of pile bib_i. Pile aia_i must not be empty when the move is made.

After all KK moves, the boxes on pile 11 must be in the order 1,2,…,n1,2,\ldots,n from bottom to top, and piles 22 and 33 must be empty. If the boxes are already stacked this way at the beginning, you may output K=0K=0.

If there are multiple solutions, output any of them.

2 1000
2 1
4
1 2
1 3
2 1
3 1
3 1000
3 1 2
7
1 2
1 3
1 2
3 1
2 3
2 1
3 1
4 1000
1 2 3 4
0

Hint

Clarification of the first example

The box with number 11 is on top, above the box with number 22. Ana first moves box 11 to pile 22, and then box 22 to pile 33. After that, she puts box 11 back on pile 11, and then box 22 on top of it, so that box 11 is at the bottom and box 22 is at the top.

Clarification of the second example

We write the contents of each pile as a sequence of numbers from bottom to top. At the beginning, pile 11 contains [3,1,2][3,1,2], so the box with number 22 is on top, and piles 22 and 33 are empty. The table shows the state of all piles after each move:

Move Pile 1 Pile 2 Pile 3
1→21 \to 2 [3,1][3,1] [2][2] [ ][\,]
1→31 \to 3 [3][3] [1][1]
1→21 \to 2 [ ][\,] [2,3][2,3]
3→13 \to 1 [1][1] [ ][\,]
2→32 \to 3 [2][2] [3][3]
2→12 \to 1 [1,2][1,2] [ ][\,]
3→13 \to 1 [1,2,3][1,2,3] [ ][\,]

After the seventh move, pile 11 contains boxes 1,2,31,2,3 from bottom to top, and piles 22 and 33 are empty, so the arrangement is correct. This is not the only correct sequence of moves, nor is it the shortest one.

Clarification of the third example

The boxes on pile 11 are already in the order 1,2,3,41,2,3,4 from bottom to top, and piles 22 and 33 are empty. This is exactly the arrangement Ana wants, so she does not need to make any moves, and K=0K=0 is a correct answer.

Scoring

Subtask Points Constraints
1 18 n≤5n \le 5, M=1000M=1000
2 31 M=260 000M=260\,000
3 54 M=12 500M=12\,500
4 7 M=5000M=5000