求助 后一半全WA

P1908 逆序对

AbsoluDe @ 2022-11-10 21:56:03

//倒序数
#include <iostream>
using namespace std;

int n;
long long across(long long *A,int p,int q,int r);
long long Merge(long long *A,int p,int r);
int main(){
    int i = 0;
    cin >> n;
    long long A[500010];
    for (int j = 0; j < n; j++){
        scanf("%lld", &A[j]);
    }
    printf("%lld", Merge(A, 0, n-1));
    return 0;
}

long long Merge(long long *A,int p,int r){
    if (p < r){
        int q = (p+r) / 2;
        return Merge(A, p, q) + Merge(A, q+1, r) + across(A, p, q, r);
    }
    else{
        return 0;
    }
}

long long across(long long *A, int p, int q, int r){
    int n1 = q - p + 1, n2 = r - q;
    long long L[n1], R[n2];
    int cnt = 0;
    for (int i = 0; i < n1; i++){
        L[i] = A[p+i];
    }
    for (int j = 0; j < n2; j++){
        R[j] = A[q+j+1];
    }
    int pl = 0, pr = 0, i = p;
    while (pl < n1 && pr < n2){
        if (L[pl] >= R[pr]){
            A[i] = R[pr++];
        }
        else {
            A[i] = L[pl++];
            cnt += pr;
        }
        i++;
    }
    while (pl < n1){
        A[i] = L[pl++];
        cnt += pr;
        i++;
    }
    while (pr < n2){
        A[i] = R[pr++];
        i++;
    }
    return cnt;
}

by CooooldWind_ @ 2022-11-28 15:52:57

?我没开long long见祖宗那你是啥


by CooooldWind_ @ 2022-11-28 15:56:07

还有你这真是归并?


|