萌新求助0分,黑压压的一片...

P1908 逆序对

cstdios @ 2020-02-05 12:54:16

#include <iostream>
#include <cstdio>
using namespace std;
const int INF=5000005;
int n,a[INF],b[INF];
long long ans;
void msort(int l,int r)
{
    if (l==r) return ;
    int Mid=(l+r)>>1;
    msort(l,Mid);
    msort(Mid+1,n);
    int i=l,j=Mid+1,k=l;
    while (i<=Mid && j<=r)
    {
        if (a[i]>a[j])
        {
            b[k++]=a[j++];
            ans+=(long long ) Mid-i+1;
        }
        else {
            b[k++]=a[i++];
        }
    }
    while (i<=Mid) b[k++]=a[i++];
    while (j<=r) b[k++]=a[j++];
    for (i=l;i<=r;i++) a[i]=b[i];
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    msort(1,n);
    printf("%lld\n",ans);
    return 0;
}

by Resonaa @ 2020-02-05 13:17:57

@cstdios

msort(Mid+1,n);

改成

msort(Mid+1,r);

就可以AC了。


by cstdios @ 2020-02-05 13:19:58

@kevinhou 非常感谢大佬的提醒


by jijidawang @ 2020-02-24 13:22:38

@cstdios 黑压压的一片TLE?


by cstdios @ 2020-02-24 13:27:53

@jijidawang MLE+TLE


|