The number of adjacent values can be exchanged, and the number of consecutive values is not decreased after the number of times is exchanged .. if each number is smaller than the number on the left, it is exchanged with the number on the left .. a value greater than the number on the right is exchanged with the number on the right... in this way, the answer is equivalent to the number of reverse orders of this series... if the reverse Number of brute force queries is O (n ^ 2... for this question data. unacceptable... the idea of sub-governance... count the number of reverse orders of edge sorting in the Merge Sorting Process... program:
# Include <iostream> # include <stdio. h> # include <string. h> # include <set> # include <ctime> # include <algorithm> # include <queue> # include <cmath> # include <map> # define oo 100000007 # define ll long long # define pi acos (-1.0) # define MAXN 500005 using namespace std; int n, a [MAXN], temp [MAXN]; ll ans; void merge (int s1, int e1, int s2, int e2) {int x, I, j, num = 0; I = s1, j = s2; for (x = s1; x <= e2; x ++) {if (I> e1) temp [x] = a [j ++]; else I F (j> e2) temp [x] = a [I ++]; else if (a [I]> a [j]) {temp [x] = a [j ++]; ans ++ = e1-s1 + 1-num; // The number of inserted segments from the back is inserted in front of the number of segments ...} else temp [x] = a [I ++], num ++;} for (I = s1; I <= e2; I ++) a [I] = temp [I]; return;} void merge_sort (int l, int r) {if (l = r) return; int mid = (l + r) /2; merge_sort (l, mid); merge_sort (mid + 1, r); merge (l, mid, mid + 1, r); return;} int main () {int I; while (~ Scanf ("% d", & n) {if (! N) break; for (I = 1; I <= n; I ++) scanf ("% d", & a [I]); ans = 0; merge_sort (1, n); printf ("% I64d \ n", ans);} return 0 ;}