diff --git a/22.cpp b/22.cpp new file mode 100644 index 0000000..3a83263 --- /dev/null +++ b/22.cpp @@ -0,0 +1,40 @@ +// 107 +#include +using namespace std; +int n; +int a[500005], tmp[500005]; +long long ms(int l, int r) +{ + if (l == r) + return 0; + int mid = l + r >> 1; + long long ans = 0; + ans = ms(l, mid) + ms(mid + 1, r); + int i = l, j = mid + 1, tot = l; + while (i <= mid && j <= r) + { + if (a[i] > a[j]) + tmp[tot++] = a[j++], ans+=j-tot; + else + tmp[tot++] = a[i++]; + } + while (i <= mid) + tmp[tot++] = a[i++]; + while (j <= r) + tmp[tot++] = a[j++]; + for (int i = l; i <= r; ++i) + a[i] = tmp[i]; + return ans; +} +int main() +{ + scanf("%d", &n); + while (n != 0) + { + for (int i = 1; i <= n; ++i) + scanf("%d", &a[i]); + printf("%lld\n", ms(1, n)); + scanf("%d", &n); + } + return 0; +} \ No newline at end of file