Hgvuryg27 @ 2024-01-31 23:03:42
刚学归并,已经找不到哪里有问题了(?)
#include <iostream>
#include <cstring>
using namespace std;
int a[500001];
int tmp[500001];
long long count = 0;
void merge(int begin1,int end1,int begin2,int end2)
{
int index = 0;
int i=begin1,j=begin2;
while(i<=end1&&j<=end2)
{
if(a[i]<=a[j])
{
tmp[index++] = a[i];
i++;
}
else
{
count+=end1-i+1;
tmp[index++] = a[j];
j++;
}
while(i<=end1)
tmp[index++] = a[i++];
while(j<=end2)
tmp[index++] = a[j++];
memcpy(a+begin1,tmp,sizeof(int)*index);
}
}
void gbsort(int begin,int end)
{
if(begin>=end)
return;
int mid=(begin+end)/2;
gbsort(begin,mid);
gbsort(mid+1,end);
merge(begin,mid,mid+1,end);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
cin>>a[i];
gbsort(0,n-1);
cout<<count;
return 0;
}
求大神帮帮忙看看错误!! :(
by cxlian25 @ 2024-02-01 01:10:00
merge的大括号标错位置了呢()
void merge(int begin1,int end1,int begin2,int end2)
{
int index = 0;
int i=begin1,j=begin2;
while(i<=end1&&j<=end2)
{
if(a[i]<=a[j])
{
tmp[index++] = a[i];
i++;
}
else
{
count+=end1-i+1;
tmp[index++] = a[j];
j++;
}
}
while(i<=end1)
tmp[index++] = a[i++];
while(j<=end2)
tmp[index++] = a[j++];
memcpy(a+begin1,tmp,sizeof(int)*index);
}
by Hgvuryg27 @ 2024-02-06 18:33:27
@cxlian25 懂了懂了!谢谢dalao