Powered by:AB_IN 局外人
P1908 逆序对
对于给定的一段正整数序列,逆序对就是序列中 ai>aj
且i<j
的有序对。
其实就是冒泡排序时,将数组升序排列时交换的次数。但n≤5×10^5 ,O(n^2)肯定不行 。看题解 可以用树状数组做,但菜鸡还没学。
so,归并排序。(跑了接近一秒,不是最优解哈)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#pragma GCC optimize(2)
#pragma GCC optimize(3)
const ll maxn=1e6+10;
ll n,cnt,a[maxn],b[maxn];
char buf[1 << 21], *p1=buf, *p2=buf;
inline ll getc(){
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline ll read() {
ll ret = 0,f = 0;char ch = getc();
while (!isdigit (ch)) {
if (ch == '-') f = 1;
ch = getc();
}
while (isdigit (ch)) {
ret = ret * 10 + ch - 48;
ch = getc();
}
return f ? -ret : ret;
}
inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); }
void _merge(ll l,ll mid,ll r)
{
ll p1=l,p2=mid+1;
for(ll i=l;i<=r;i++){
if((p1<=mid) && ((p2>r) || a[p1] <= a[p2])){
b[i]=a[p1];
p1++;
}
else{
b[i]=a[p2];
p2++;
cnt+=mid-p1+1;//最关键的一句
}
}
for(ll i=l;i<=r;i++) a[i]=b[i];
}
void erfen(ll l,ll r)
{
ll mid=(l+r)/2;
if(l<r){
erfen(l,mid);
erfen(mid+1,r);
}
_merge(l,mid,r);
}
int main()
{
n=read();
for(ll i=1;i<=n;i++)
a[i]=read();
erfen(1,n);
write(cnt);putchar(10);
return 0;
}
归并排序的原理要是知道了,那是什么时候像冒泡排序一样消除的逆序对呢?
对,就是这个时候。
else{
b[i]=a[p2];
p2++;
cnt+=mid-p1+1;//最关键的一句
}
什么意思呢,看下面的图,就是让a数组右区间在p2的值提到了前面(左区间),而红色涂满的区间都与p2指针指的数构成逆序对,故求区间长度就可以了。
完结。