Lucifero @ 2020-07-19 13:14:52
#include <bits/stdc++.h>
using namespace std;
int s1[2000],s2[2000];
long long ans;
void merge(int l,int mid,int r)
{
if (l==r) return;
merge(l,(l+mid)/2,mid);
merge(mid+1,(mid+1+r)/2,r);
int i=l,j=mid+1,k=l;
while(i<=mid && j<=r)
{
if (s1[i]<=s1[j]) s2[k++]=s1[i++];//从大到小">",从小到大"<"
else
{
s2[k++]=s1[j++];
ans+=mid-i+1;
}
}
while(i<=mid) s2[k++]=s1[i++];
while(j<=r) s2[k++]=s1[j++];
for(i=l;i<=r;i++) s1[i]=s2[i];
}
/*
void MergeSort(int L,int R)
{
int Mid;
if(L<R)
{
Mid=(L+R)/2;
MergeSort(L,Mid);
MergeSort(Mid+1,R);
merge(L,Mid,R);
}
}
*/
int main()
{
//逆序对
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&s1[i]);
merge(1,(1+n)/2,n);
printf("%lld",ans);
}
by zhanghzqwq @ 2020-07-19 13:17:45
数组开小了
by zhanghzqwq @ 2020-07-19 13:17:54
@Gray_White
by Lucifero @ 2020-07-19 13:20:58
@zhanghanzhang OK,谢谢
by chenxia25 @ 2020-07-19 13:22:37
热可还行