50分求助!

P1908 逆序对

lucus_Fu0202 @ 2024-05-04 15:10:44

#include<bits/stdc++.h>
using namespace std;
long long r[100001],a[100001],n,ans;
void msort(int s , int t){
    if(s == t){
        return;
    }
    int mid = (s + t) / 2;
    msort(s , mid);
    msort(mid + 1 , t);
    int i = s , j = mid + 1 , k = s;
    while(i <= mid && j <= t){
        if(a[i] <= a[j]){
            r[k] = a[i];
            k++;
            i++;
        }else{
            r[k] = a[j];
            k++;
            j++;
            ans += mid - i + 1;
        }
    }
    while(i <= mid){
        r[k] = a[i];
        k++;
        i++;
    }
    while(j <= t){
        r[k] = a[j];
        k++;
        j++;
    }
    for(int i = s ; i <= t ; i++){
        a[i] = r[i];
    }
}
int main(){
    cin>>n;
    for(int i = 1 ; i <= n ; i++){
        cin>>a[i];
    }
    msort(1 , n);
    cout<<ans;
    return 0;
}

by lucus_Fu0202 @ 2024-05-04 22:04:48

不用了,已发现是范围错误。


|