1 #include <iostream> 2 #include <stdio.h> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 int high[100010]; 7 int temp[100010]; 8 int angry[100010]; 9 int num; 10 void mergesort(int first,int mid,int last) 11 { 12 int left1=first,right1=mid,left2=mid+1,right2=last; 13 int i=0; 14 while(left1<=right1&&left2<=right2) 15 { 16 if(high[left1]<=high[left2]) 17 { 18 temp[i++]=high[left1++]; 19 } 20 else 21 { 22 temp[i++]=high[left2++]; 23 angry[left2]+=right1-left1+1; 24 num+=right1-left1+1; 25 for(int j=left1; j<=right1; j++) 26 { 27 angry[j]++; 28 } 29 30 } 31 } 32 if(left1>right1) 33 { 34 while(left2<=right2) 35 { 36 temp[i++]=high[left2++]; 37 } 38 } 39 else if(left2>right2) 40 { 41 while(left1<=right1) 42 { 43 temp[i++]=high[left1++]; 44 } 45 } 46 for(int i=0; i<last-first+1; i++) 47 { 48 high[first+i]=temp[i]; 49 } 50 51 } 52 void Sort(int first,int last) 53 { 54 if(first<last) 55 { 56 int mid=(first+last)/2; 57 Sort(first,mid); 58 Sort(mid+1,last); 59 mergesort(first,mid,last); 60 } 61 } 62 int main() 63 { 64 int n; 65 while(scanf("%d",&n)!=EOF) 66 { 67 num=0; 68 for(int i=0; i<n; i++) 69 scanf("%d",&high[i]); 70 Sort(0,n-1); 71 for(int i=0; i<n; i++) 72 { 73 printf("%d ",angry[i]); 74 } 75 cout<<endl; 76 cout<<num<<endl; 77 } 78 79 return 0; 80 }
时间: 2024-12-18 18:00:59