样例已过,全wa,求助

P1908 逆序对

orangeSteve_dev @ 2024-09-22 10:40:25

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int M=5e5+5;
ll n;
ll a[M],temp[M];
ll ans=0;
void mSort(ll l,ll r) {
    if(l>=r)return ;
    ll mid=(l+r)/2,i,j,c=0;
    mSort(l,mid);
    mSort(mid+1,r);
    i=l;
    j=mid+1;
    while(i<=mid && j<=r) {
        if(a[i]<a[j]) {
            temp[++c]=a[i++];
        } else {
            temp[++c]=a[j++];
            ans+=mid-i+1;
        }
    }
    while(i<=mid) {
        temp[++c]=a[i++];
    }
    while(j<=r) {
        temp[++c]=a[j++];
    }
    for(i=1; i<=n; i++) {
        a[l++]=temp[i];
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n; 
    for(ll i=1; i<=n; i++) {
        cin>>a[i];
    }
    mSort(1,n);
    cout<<ans<<"\n";
}

by ZMQ_Ink6556 @ 2024-09-22 10:53:54

@orangeSteve_dev 样例已过,全 WA 求调

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << "11";
    return 0;
}

by zzbzwjx @ 2024-09-22 18:04:09

void mSort(ll l,ll r) {
    if(l>=r)return ;
    ll mid=(l+r)/2,i,j,c=0;
    mSort(l,mid);
    mSort(mid+1,r);
    i=l;
    j=mid+1;
    while(i<=mid && j<=r) {
        if(a[i]<a[j]) {
            temp[++c]=a[i++];
        } else {
            temp[++c]=a[j++];
            ans+=mid-i+1;
        }
    }
    while(i<=mid) {
        temp[++c]=a[i++];
    }
    while(j<=r) {
        temp[++c]=a[j++];
    }
    for(i=1; i<=n; i++) {
        a[l++]=temp[i];
    }
}

改为

void mSort(ll l,ll r) {
    if(l>=r)return ;
    ll mid=(l+r)/2,i,j,c=0;
    mSort(l,mid);
    mSort(mid+1,r);
    i=l;
    j=mid+1;
    while(i<=mid && j<=r) {
        if(a[i]<=a[j]) {//This!------------
            temp[++c]=a[i++];
        } else {
            temp[++c]=a[j++];
            ans+=mid-i+1;
        }
    }
    while(i<=mid) {
        temp[++c]=a[i++];
    }
    while(j<=r) {
        temp[++c]=a[j++];
    }
    for(i=1; i<=c; i++) {//This!-----------
        a[l++]=temp[i];
    }
}

by zzbzwjx @ 2024-09-22 18:05:52

@orangeSteve_dev 注意细节


|