By Arvin Liu

# Ghost Leg

## 1-6 Sub-problems

1.  inversion的演算法。
2.  inversion的演算法的複雜度。
3.  # inversion = |bubble sort|。
--------
4.  條件全滿的可控鬼腳圖
5.  條件非全滿的可控鬼腳圖

# Inversion Pair

## 1-6-1 求逆序數對

// Count inversion pair between A[l~m] and A[m+1~r]
// A[l~m] and A[m+1~r] are sorted due to it's merged before by merge sort.
void counting(int l, int m, int r){
int r_ptr = m+1;
for(int i=l; i<=m; i++){
while(r_ptr <= r && A[r_ptr] < A[i])
r_ptr ++;
ans += r_ptr - m - 1;
}
}

## 1-6-1 求逆序數對

l_1 <= l_2 <= l_3 ... \\ r_1 <= r_2 <= r_3 ...

## 1-6-2 求逆序數對時間複雜度

T(n) = 2T(\frac n2) + O(n)
T(n) = O(n \log n)

## 1-6-3 proof |Bubble sort| = # Inversion

a_1, a_2, a_3, ... a_i, a_{i+1}, ... a_n

a_i > a_{i+1}

I'(A) = I(A)-1

# Ghost Leg

1

2

3

4

A

B

C

D

## (C-> 1), (D->3)}

Step1 : 把數字對回去。

4

2

1

3

Step2 :對對回去的數列做逆序數對。

4

2

1

3

2

4

1

3

2

1

4

3

2

1

3

4

1

2

3

4

1

2

3

4

A

B

C

D

## Constraints: {(A->4), (B->2)}

Step1 : 把數字對回去。

4

2

?

?

Step2 : 把沒有條件的從小填到大。

Step 3: 對它做逆序數對。

## 假設條件：

x_1, x_2, x_3, ?_4, x_5, ?_6, ?_7

## 1-6-5 Why?

### 我們隨便抓x_i, ?_j, ?_k。 如果i < j&k 或 i> j&k則不變，

x_1, x_2, x_3, ?_4, x_5, ?_6, ?_7

# Case: Single Block

## 1-7-1 證明單block有解

\log_{2} \frac Nk \\ \frac {i-1}{k}

is non-negative integer. 且

is non-negative integer

* N/2 是 k的倍數。

## 1-7-3 單格翻完狀態O(d1+d2)

\sum ^{ log_2(d_1+d_2) }_{x=0} 2^x = 2^{log_2(d_1+d_2) + 1 } - 1 = 2 \times (d_1+d_2) - 1 = O(d_1 + d_2)

# Case: Multi Blocks

## 1-7-4 DP

for now_B in B:
for (x,y) in possibilities_of_unfold(now_B):
if DP[x-1] is True:
DP[y] = Ture

## 1-7-6 DP複雜度

d_1
d_2
d_3

...

2(d_1+d_2)-1
2(d_2+d_3)-1

...

\}
4 \sum_{i=1}^n d_i - 2d_1 - 2d_n - n = O(N)
\Omega(N) \text{Tirivial. (最多要掃N次)}