~~~ Question Link ~~~
A sequence is given, and it is required to find the sequence and the smallest in the reverse order number pair after it passes through the ring shift.
Idea; use the line segment tree to calculate the number of reverse orders. Whenever a number X is added, X ~ is obtained ~ N-1 indicates the number of Reverse Order pairs.
After a set of sequences are obtained, the inverse number pair after the ring shift can be obtained using the formula, and the ring movement is implemented using STL.
Code:
# Include <stdio. h> # include <string. h >#include <algorithm> # define n 5002 using namespace STD; int T [4 * n]; void Update (int c, int L, int R, int W) {int M = L + (R-l)/2; If (L = r) {T [c] = 1; return;} If (W <= m) update (2 * C, L, M, W); else update (2 * C + 1, m + 1, R, W ); T [c] = T [2 * C] + T [2 * C + 1];} int query (int c, int L, int R, int lf, int RT) {int sum = 0, M = L + (R-l)/2; If (LF <= L & RT> = r) return T [c]; if (LF <= m) S Um + = query (2 * C, L, M, lf, RT); If (RT> m) sum + = query (2 * C + 1, m + 1, r, lf, RT); Return sum;} int main () {int I = 0, n = 0, sum = 0, min = 0, num [N]; while (scanf ("% d", & N )! = EOF) {sum = 0; memset (T, 0, sizeof (t); for (I = 0; I <n; I ++) {scanf ("% d", & num [I]); sum + = query (1, 0, n-1, num [I], n-1); Update (1, 0, 0, n-1, num [I]);} min = sum; for (I = 0; I <n-1; I ++) {sum = sum-num [0] + (n-1) -num [0]; rotate (Num, num + 1, num + n); If (min> sum) min = sum;} printf ("% d \ n ", min);} return 0 ;}