new file: 22.cpp
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
// 107
|
||||
#include <bits/stdc++.h>
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user