input:Sophist.in output:Sophist.out
时间限制: 1000 ms 空间限制: 60000 KB 具体限制
1 #include <bits/stdc++.h> 2 using namespace std; 3 long int answer=0,a[5*100005],r[5*100005]; 4 void msort(int s,int t) 5 { 6 if(s==t) return; 7 int mid=(s+t)/2; 8 msort(s,mid); 9 msort(mid+1,t); 10 int i=s,j=mid+1,k=s; 11 while(i<=mid && j<=t) 12 { 13 if(a[i]<=a[j]) 14 { 15 r[k]=a[i];k++;i++; 16 }else{ 17 r[k]=a[j];k++;j++; 18 answer+=mid-i+1; 19 } 20 } 21 while(i<=mid) 22 { 23 r[k]=a[i];k++;i++; 24 } 25 while(j<=t) 26 { 27 r[k]=a[j];k++;j++; 28 } 29 for(int i=s;i<=t;i++) a[i]=r[i]; 30 } 31 int main(){ 32 freopen("Sophist.in","r",stdin); 33 freopen("Sophist.out","w",stdout); 34 int n; 35 cin>>n; 36 for(int i=1;i<=n;i++) 37 cin>>a[i]; 38 msort(1,n); 39 cout<<answer; 40 }