Leaf of the upper tree is joined to leaf p_i of the lower one, and we may swap the two subtrees of any vertex of the upper tree, as many times as we like. We need the fewest crossings between the segments that such swaps can reach.
Segments from leaves i < j cross exactly when p_i > p_j, so the crossings are the inversions of . A swap at a vertex whose halves have leaves each moves both halves as blocks. Pairs inside a half and pairs with a leaf outside the vertex stay as they are. Of the L^2 pairs with one leaf in each half, the that crossed stop crossing and the other L^2 - c start, so the vertex contributes \min(c, L^2 - c). The number depends only on which values sit in each half, not on their order, so no vertex's choice affects another's. The answer is the sum of \min(c, L^2 - c) over all vertices, and merge sort counts every in one pass, in O(N \cdot 2^N).
Put the leaves of both trees on two lines, numbered from left to right. Take two leaves i < j of the upper tree. If p_i < p_j, their segments go down in the same order and don't meet. If p_i > p_j, they swap sides on the way down and cross once. So the number of crossings is the number of pairs i < j with p_i > p_j, the inversions of the sequence . In the sample, is 3 2 1 8 5 4 7 6, with 9 inversions and 9 crossings.
The direct way tries every set of swaps. A tree of depth has 2^N - 1 vertices, so there are 2^{2^N - 1} sets, already 32 768 for N = 4. We need to see what a single swap does.
Take a vertex whose two halves have leaves each, and swap them. Look at any two leaves.
Both in the same half. The half moves as one block, so the two leaves keep their order and their segments cross as before.
One inside the vertex and one outside. The outside leaf stays to the left (or to the right) of every leaf of the vertex, so nothing changes.
One in each half. Before the swap the leaf of the left half came first, and after it the leaf of the right half does. So every such pair that crossed now doesn't, and every pair that didn't now crosses.
There are L \cdot L pairs with one leaf in each half. If of them cross, after the swap L^2 - c of them do. The vertex should swap exactly when that is smaller, and it contributes \min(c, L^2 - c) crossings.
The number counts pairs (a value in the left half, a value in the right half) where the left value is larger. It depends only on which values are in each half, not on their order. Swaps inside a half only reorder that half, and swaps above the vertex move it as one block, so never changes. Each vertex can therefore choose its best option on its own, and the answer is the sum of \min(c, L^2 - c) over all vertices.
In the sample, the root's halves hold 3 2 1 8 and 5 4 7 6. Only 8 is larger than values on the right, so c = 4 out of 16, and the root keeps its halves. Its left child, with 3 2 and 1 8, has c = 2 of 4, and either choice gives 2. The three vertices just above the leaves 3 2, 5 4 and 7 6 each have one crossing pair out of one, so each of them swaps and the crossing disappears. The answer is 4 + 2 = 6.
As given, 9 pairs of segments cross.
Fig. 1. The sample before and after the three swaps that remove a crossing each.
Counting the pairs of every vertex one by one costs L^2 per vertex, more than 10^{11} steps altogether for N = 19. Sorting does it faster. If both halves of a vertex are sorted, merge sort merges them by always taking the smaller front value. Whenever it takes a value from the right half, every value still waiting in the left half is larger, and each of them forms one pair. Adding up these counts during the merge gives for free.
Merge sort sorts the halves the same way, one level further down, so one sort of the whole sequence finds for every vertex of the tree. The tree's vertices are exactly the blocks that merge sort splits the sequence into.
The C++ program sorts recursively, as merge sort is usually written. The Python program merges level by level, from blocks of one leaf up to the whole sequence, which avoids recursion and lets sorted do the merging quickly.
#include <iostream>
#include <vector>
using namespace std;
vector<int> p, tmp;
long long answer = 0;
// sorts p[l..r) and adds the fewest crossings between its two halves
void solve(int l, int r) {
if (r - l == 1) return;
int m = (l + r) / 2;
solve(l, m);
solve(m, r);
// merge the two sorted halves, counting pairs where the left value is larger
long long cross = 0;
int i = l, j = m, k = l;
while (i < m || j < r) {
if (j == r || (i < m && p[i] < p[j])) {
tmp[k++] = p[i++];
} else {
cross += m - i; // p[j] is smaller than every value left in the left half
tmp[k++] = p[j++];
}
}
for (int x = l; x < r; x++) p[x] = tmp[x];
long long half = m - l;
answer += min(cross, half * half - cross); // keep the halves or swap them
}
int main() {
int n;
cin >> n;
int leaves = 1 << n;
p.resize(leaves);
tmp.resize(leaves);
for (int& x : p) cin >> x;
solve(0, leaves);
cout << answer << '\n';
}Each of the levels of merging looks at every leaf a constant number of times, so the program runs in O(N \cdot 2^N) time, about 10^7 steps for N = 19, and O(2^N) memory.
intWith 2^{19} leaves there are about 1.4 \cdot 10^{11} pairs, and a shuffled input leaves about 7 \cdot 10^{10} crossings after the best swaps. Keep cross and the answer in long long.
Comparing every value of the left half with every value of the right half is correct but takes more than 10^{11} steps for N = 19, and the program gets Time limit exceeded. Count the pairs while merging.