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 09:56:22
@iTwinkle
by liqingyang @ 2020-02-17 09:57:51
你可以一个一个注释,直到没有死循环,就知道哪里有死循环了
by liqingyang @ 2020-02-17 09:57:57
@iTwinkle
by Krystallos @ 2020-02-17 09:58:47
好吧去掉第三个,我看错了
by liqingyang @ 2020-02-17 09:59:03
哦,我知道了,应该是scanf("%d",&n)吧
by liqingyang @ 2020-02-17 10:00:00
scanf我记得必须得加&,对吧@Alfred_Tirpitz @iTwinkle
by Krystallos @ 2020-02-17 10:00:32
@liqingyang char
数组不用
by liqingyang @ 2020-02-17 10:00:41
我觉得应该再把第一个去掉
by liqingyang @ 2020-02-17 10:01:02
但他的是int啊
by Krystallos @ 2020-02-17 10:01:07
数组确实没开够啊