-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1750.cpp
More file actions
60 lines (52 loc) · 1.18 KB
/
p1750.cpp
File metadata and controls
60 lines (52 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int num[500001];
int temp[500001];
long long count=0;
void Sort(int num[],int l,int r);
void hebing(int num[],int l,int r);
int main(){
int n;
cin>>n;
count=0;
for(int i=0;i<n;i++){
cin>>num[i];
}
Sort(num,0,n-1);
cout<<count<<endl;
}
void Sort(int num[],int l,int r){
if(l<r){
int mid=(l+r)/2;
Sort(num,l,mid);
Sort(num,mid+1,r);
hebing(num,l,r);
}
}
void hebing(int num[],int l,int r){
int mid=(l+r)/2;
int t=0;
int i=l;
int j=mid+1;
while (i<=mid && j<=r){
if(num[i]<=num[j]){
temp[t++] = num[i++];
}else {
temp[t++] = num[j++];
count=count+(mid-i+1);
}
}
while(i<=mid){//将左边剩余元素填充进temp中
temp[t++] = num[i++];
}
while(j<=r){//将右序列剩余元素填充进temp中
temp[t++] = num[j++];
}
t = 0;
//将temp中的元素全部拷贝到原数组中
while(l <= r){
num[l++] = temp[t++];
}
}