This page is still under construction.

Parts of this page are still being built. What you see may change.

Skewed Sorting

Time limit1sMemory limit128 MB

Summary
Apply a recursive swap procedure on 2^N cows, comparing equal-length halves as base-2^N numbers, and report total distance moved plus final order.
Level

Medium5 of 10

Topics
Divide and conquer, Recursion, Sorting, Simulation
Solved
No attempts yet

Problem

Farmer John has 2N2^N cows (1≤N≤101 \le N \le 10), each branded on her flank with a distinct integer label in the range 1…2N1 \dots 2^N. They stand in a line in some arbitrary order: the first cow is cow1\mathrm{cow}_1, the second is cow2\mathrm{cow}_2, and so on.

He reorders them with the following recursive procedure:

  1. If the current line holds more than one cow, split it into two halves of equal length. First apply this same procedure to the left half, then apply it to the right half.
  2. Treat the two resulting halves as two equal-length numbers written in base 2N2^N, one cow per digit with the most significant digit on the left. If the second (right) number is smaller than the first (left) number, swap the two halves: the cow at each offset of the left half exchanges places with the cow at the same offset of the right half.

Because every label is distinct, the two halves can never be equal, so this comparison always resolves.

Every time two halves of length kk are swapped, all 2k2k cows involved move exactly kk positions, so that swap adds 2k22k^2 to the running total distance.

Run the procedure on the initial line, then report the total distance travelled by all cows together with the final order of the line.

As an example, consider this line of 23=82^3 = 8 cows:

8 5 2 3 4 7 1 6

First John sorts each half separately:

8 5 2 3 | 4 7 1 6

Each half still holds more than one cow, so it is split again. Starting with the left half:

8 5 | 2 3

Splitting once more gives

8 | 5      and      2 | 3

each of which is handled by rule 2, ultimately yielding

5 | 8      and      2 | 3   (unchanged)

Turning 8 5 into 5 8 moves each of the two cows one position, so the total distance becomes 22. The pair 2 3 was already ordered, so the total stays at 22. The left group now reads

5 8 | 2 3

Applying rule 2 to 5 8 versus 2 3: since 2 precedes 5, we swap the two pairs:

2 3 5 8

Each of these four cows moved two positions, adding 88, so the total becomes 1010.

Now the right group 4 7 | 1 6 splits into 4 7 and 1 6, both already ordered. Comparing them, 1 precedes 4, so we swap:

1 6 4 7

adding another 88 and bringing the total to 1818.

The line now looks like this, ready for the final application of rule 2 to the two groups of four:

2 3 5 8 | 1 6 4 7

Since 1 precedes 2, we swap the halves:

1 6 4 7 2 3 5 8

Each of the eight cows moved four positions, adding 3232 and making the total 5050.

So the answer is a distance of 5050 and the final line 1 6 4 7 2 3 5 8.

Input

  • Line 1: a single integer NN.
  • Lines 2 to 2N+12^N + 1: line i+1i + 1 contains a single integer, the label of cowi\mathrm{cow}_i.

Output

  • Line 1: a single integer, the total distance travelled by all the cows.
  • Lines 2 to 2N+12^N + 1: line i+1i + 1 contains a single integer, the ii-th cow in the final line.

Examples1

  1. Example 1

    Input
    3
    8
    5
    2
    3
    4
    7
    1
    6
    
    Expected output
    50
    1
    6
    4
    7
    2
    3
    5
    8