求助

P1908 逆序对

iTwinkle @ 2020-02-17 09:42:07

juruo求助,是哪里错了导致死循环

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN=1e5+10;
int a[MAXN],temp[MAXN];
LL ans=0;
void merge(int l,int r)
{
    if(l>=r)    return;
    int mid=(l+r)/2;
    merge(l,mid);
    merge(mid+1,r);

    int i=l,j=mid+1,k=l;
    while(i<=mid&&j<=r)
    {
        if(a[i]>a[j])
        {
            temp[k]=a[i];
            k++;i++;ans+=mid-i+1;
        }
        else
        {
            temp[k]=a[j];
            k++;j++;
        }
    }
    while(i<=mid)
    {
        temp[k]=a[i];
        k++;i++;
    }
    while(j<=r)
    {
        temp[k]=a[j];
        k++;j++;
    }
    for(int i=l;i<=r;i++)   a[i]=temp[i];
    return;
}
int main()
{
    int n; scanf("%d",n);
    for(int i=1;i<=n;i++)
        scanf("%d",a+i);
    merge(1,n);
    cout<<ans;
    return 0;
}

by Krystallos @ 2020-02-17 10:11:13

@liqingyang 数组没开够


by iTwinkle @ 2020-02-17 10:14:29

@Alfred_Tirpitz @liqingyang 谢谢了,我去改代码,感谢纠错


by liqingyang @ 2020-02-17 10:18:27

不客气


by liqingyang @ 2020-02-17 10:18:37

@Alfred_Tirpitz 是的


by liqingyang @ 2020-02-17 10:19:02

改成5e5+10应该就行了


上一页 |