帅皮的瓜粉 @ 2020-03-16 16:28:21
代码
#include<bits/stdc++.h>
using namespace std;
int a[1000000],tmp[1000000];
long long s=0;
void mergeArray(int l,int r,int mid)
{
int i=l,j=mid+1;
int n=mid,m=r;
int x=0;
while(i<=n&&j<=m)
{
if(a[i]<=a[j])
{
tmp[x++]=a[i++];
}
else
{
tmp[x++]=a[j++];
s+=mid-i+1;
}
}
while(i<=n)
{
tmp[x++]=a[i++];
}
while(j<=n)
{
tmp[x++]=a[j++];
}
for(int i=0;i<x;i++)
{
a[l+i]=tmp[i];
}
}
void mergeSort(int l,int r)
{
if(l<r)
{
int mid=(r+l)/2;
mergeSort(l,mid);
mergeSort(mid+1,r);
mergeArray(l,r,mid);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergeSort(0,n-1);
printf("%d ",s);
return 0;
}
luogu.com.cn/record/31816508
by 帅皮的瓜粉 @ 2020-03-16 16:42:20
@XCGL https://www.luogu.com.cn/record/31817118
by NujObIuc @ 2020-03-16 16:42:22
@谢哥二号
#include<bits/stdc++.h>
using namespace std;
int a[1000000],tmp[1000000];
long long s=0;
void mergeArray(int l,int r,int mid)
{
int i=l,j=mid+1;
int n=mid,m=r;
int x=0;
while(i<=n&&j<=m)
{
if(a[i]<=a[j])
{
tmp[x++]=a[i++];
}
else
{
tmp[x++]=a[j++];
s+=mid-i+1;
}
}
while(i<=n)
{
tmp[x++]=a[i++];
}
while(j<=n)
{
tmp[x++]=a[j++];
}
for(int i=0;i<x;i++)
{
a[l+i]=tmp[i];
}
}
void mergeSort(int l,int r)
{
if(l<r)
{
int mid=(r+l)/2;
mergeSort(l,mid);
mergeSort(mid+1,r);
mergeArray(l,r,mid);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergeSort(0,n-1);
printf("%d ",s);
return 0;
}
by 帅皮的瓜粉 @ 2020-03-16 16:42:28
依旧是错的
by NujObIuc @ 2020-03-16 16:42:41
@谢哥二号 longlong
by liqingyang @ 2020-03-16 16:42:53
@谢哥二号 还有应该是printf("%lld",s);呀
by NujObIuc @ 2020-03-16 16:43:49
by 帅皮的瓜粉 @ 2020-03-16 16:45:57
谢谢各位巨佬们,终于AC了!!!
by Jmakers @ 2020-08-01 22:31:01
@XCGL 谢谢巨佬