P1908 逆序对

MaLeBron @ 2023-08-17 10:46:10

#include<bits/stdc++.h>
using namespace std;
const int N=10100100;
int n,a[N],b[N],ans=0;

void Merge(int L,int mid,int R){
    int i=L,j=mid+1,k=0;
    while(i<=mid&&j<=R){
        if(a[i]<=a[j]){
            b[k++]=a[i++];
        }else{
            b[k++]=a[j++],ans+=mid-i+1;
        }
    }
    while(i<=mid)b[k++]=a[i++];
    while(j<=R)b[k++]=a[j++];
    for(int i=L,k=0;i<=R;i++){
        a[i]=b[k++];
    }
}
void mergesort(int L,int R){
    if(L<R){
        int mid=(L+R)/2;
        mergesort(L,mid);
        mergesort(mid+1,R);
        Merge(L,mid,R);
    }
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    mergesort(1,n);
        cout<<ans;
    return 0;
} 

悬关


by MaLeBron @ 2023-08-17 10:57:52

A了


|