题解
本质:求一个数前面有几个数大于它,我们把序列分成几段,然后对每段分别进行排序,然后找出这个数在前面已经排好序中的序列里有几个大于它
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll a[500005], b[500005], c[500005]; // a-original, b-replace, c-new
ll ans = 0;
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-')flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x)
{
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void bg(ll l, ll r)
{
if (l == r) return;
ll mid = (l + r) / 2;
bg(l, mid);
bg(mid + 1, r);
ll p1 = l, p2 = mid + 1, p3 = l;
while (p1 <= mid && p2 <= r)
{
if (c[p1] <= c[p2]) b[p3++] = c[p1++];//为什么要等于?因为等于不算逆序对
else
{
b[p3++] = c[p2++];
ans += mid - p1 + 1;
}
}
while (p1 <= mid) b[p3++] = c[p1++];
while (p2 <= r) b[p3++] = c[p2++];
for (ll i = l; i <= r; i++) c[i] = b[i];
return;
}
int main()
{
ll n;
read(n);
for (ll i = 1; i <= n; i++)
{
read(a[i]);
c[i]=a[i];
}
bg(1, n);
write(ans);
return 0;
}
标签:bg,P1908,ll,mid,long,while,500005,逆序
From: https://www.cnblogs.com/pure4knowledge/p/18132744