forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <cstdio> | ||
|
||
#define INF 1000000000 | ||
#define ELEMENT_COUNT 100000 | ||
|
||
using namespace std; | ||
|
||
int n, d[ELEMENT_COUNT], ans; | ||
|
||
void merge(int o1, int l1, int o2, int l2) | ||
{ | ||
static int a1[ELEMENT_COUNT], a2[ELEMENT_COUNT]; | ||
int k1 = 0, k2 = 0; | ||
for (int i = 0; i < l1; i++) | ||
{ | ||
a1[i] = d[i + o1]; | ||
} | ||
a1[l1] = INF; | ||
for (int i = 0; i < l2; i++) | ||
{ | ||
a2[i] = d[i + o2]; | ||
} | ||
a2[l2] = INF; | ||
for (int i = 0; i < l1 + l2; i++) | ||
{ | ||
if (a1[k1] <= a2[k2]) | ||
{ | ||
d[i + o1] = a1[k1++]; | ||
} | ||
else | ||
{ | ||
d[i + o1] = a2[k2++]; | ||
ans += (l1 - k1); | ||
} | ||
} | ||
} | ||
|
||
void merge_sort(int l, int r) | ||
{ | ||
if (l < r) | ||
{ | ||
int mid = (l + r) >> 1; | ||
merge_sort(l, mid); | ||
merge_sort(mid + 1, r); | ||
merge(l, mid - l + 1, mid + 1, r - mid); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
scanf("%d", &n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
scanf("%d", &d[i]); | ||
} | ||
merge_sort(0, n - 1); | ||
printf("%d", ans); | ||
return 0; | ||
} |