#P6892. [ICPC2014 WF] Baggage

[ICPC2014 WF] Baggage

题目描述

An airline has two flights leaving at about the same time from ICPCity, one to city B and one to city A. The airline also has nn counters where passengers check their baggage. At each counter there is a pair of identical baggage bins, one for city B and one for city A.

Just before the flights depart, each pair of baggage bins is moved by a motorized cart to a sorting area. The cart always moves two bins at a time, one for city B and one for city A. After all the bins have been moved, they line up in the sorting area like this:

B A B A B A ... B A

That is, there are 2n2n baggage bins in a row, starting with a bin for city B, then one for city A, and so forth. The task now is to reorder them so all the baggage bins for city A precede the baggage bins for city B. Then the bins can be loaded on the appropriate aircraft.

The reordering is done by moving pairs of adjacent baggage bins (not necessarily B then A), again via the motorized cart. For proper balance, the cart must always carry two bins, never just one. A pair of bins must always be moved to an empty space that is at least two bins wide. On the left of the first bin are some empty spaces that can be used as needed during the reordering.

When the reordering process begins, the bin locations are numbered from 11 (initially containing the leftmost B baggage bin) to 2n2n (initially containing the rightmost A baggage bin). There are 2n2n initially empty spaces to the left of the bins, numbered from 00 to 2n+1-2n+1, as shown in Figure 1 for the case n=4n=4.

Figure 1: Initial configuration of bins and empty spaces for n=4n = 4

Given nn, find a shortest sequence of moves that will reorder the bins so that all the A bins are to the left of all the B bins. At the end of the process, it is possible that the leftmost A bin is at some location other than 11, but the bins must be adjacent in a sequence of 2n2n locations.

输入格式

The input consists of a single test case, which consists of the integer nn (3n100)(3 \leq n \leq 100).

输出格式

Display a shortest sequence of moves that will correctly reorder the bins. Each move is of the form “ff to tt”, where ff and tt are integers representing the movement of the bins in locations ff and f+1f + 1 to locations tt and t+1t + 1. If multiple solutions are possible, display any one of them.

题目大意

有一无穷长的流水线 aa,初始时 i0 和 i>2n,ai=0\forall i\le0\text{ 和 } i>2n ,a_i=0否则,若 ii 为奇数,则 ai=2a_i=2;若 ii 为偶数,则 ai=1a_i=1也就是说,ai=2a_i=2ai=1a_i=1 是交替出现的。

现需要进行若干次 如下 操作,使得 aa 中的所有 非零元素连续 的一段且所有的 11 均在 22 前面

选择两个位置 ppqq,满足 ap0,ap+10a_p\ne0,a_{p+1}\ne0aq=aq+1=0a_q=a_{q+1}=0,将 aqa_q 设为 apa_paq+1a_{q+1} 设为 ap+1a_{p+1},并且将 apa_pap+1a_{p+1} 均设为 00输出时将此操作表示为 p to qppqq 是具体的值)

最小化操作步数,并输出操作序列,出题人将用 Special Judge\text{Special Judge} 来评判您的答案的正确性。

Translator:  \rm Translator:\;hhoppitree\rm\color{black}h\color{red}hoppitree

5

8 to -1
3 to 8
6 to 3
0 to 6
9 to 0

8

10 to -1
3 to 10
14 to 3
7 to 14
0 to 7
11 to 0
4 to 11
15 to 4

提示

Time limit: 1000 ms, Memory limit: 1048576 kB.

International Collegiate Programming Contest (ACM-ICPC) World Finals 2014