-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfroshweek.cpp
More file actions
170 lines (154 loc) · 3.84 KB
/
Copy pathfroshweek.cpp
File metadata and controls
170 lines (154 loc) · 3.84 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//// C++ program to Count
//// Inversions in an array
//// using Merge Sort
//#include <bits/stdc++.h>
//using namespace std;
//
//
//int merge(vector <long long> &arr, long long temp[], int left, int mid, int right){
// int i, j, k;
// long long inv_count = 0;
//
// i = left;
// j = mid;
// k = left;
// while ((i <= mid - 1) && (j <= right)) {
// if (arr[i] <= arr[j]) {
// temp[k++] = arr[i++];
// }
// else {
// temp[k++] = arr[j++];
//
// inv_count = inv_count + (mid - i);
// }
// }
//
// //This while loop is if the 1st array has remaining values
// while (i <= mid - 1)
// temp[k++] = arr[i++];
//
// //This while loop is if the 2nd array has remaining values
// while (j <= right)
// temp[k++] = arr[j++];
//
// //We need to copy all element to original arrays
// for (i = left; i <= right; i++)
// arr[i] = temp[i];
//
// return inv_count;
//}
//
//
///* An auxiliary recursive function that sorts the input array and
//returns the number of inversions in the array. */
//int _mergeSort(vector <long long> &arr, long long temp[], int left, int right)
//{
// int mid, inv_count = 0;
// if (right > left) {
//
// //1. Divide
// mid = (right + left) / 2;
//
// // 2. Recursively divide the vector
// inv_count = _mergeSort(arr, temp, left, mid); // divide the vector into a single element from start to mid
// inv_count += _mergeSort(arr, temp, mid + 1, right); // divide the vector into a single element from mid+1 to end
//
// // 3. Sort & merge the two parts
// inv_count += merge(arr, temp, left, mid + 1, right);
// }
// return inv_count;
//}
//
//
//// sorts the input array and returns the number of inversions
//int mergeSort(vector <long long> &arr, long long array_size)
//{
// long long temp[array_size];
// return _mergeSort(arr, temp, 0, array_size - 1);
//}
//
//
//
//int main()
//{
//
// long long n;
// vector<long long> arr;
//
// cin >> n;
//
// for(int i=0; i<n; i++){
// long long value;
//
// cin >> value;
// arr.push_back(value);
// }
//
// //int arr[100000];
//
// //cin >> n;
//
//
//// for(int i=0; i<n; i++){
//// //long long value;
////
//// cin >> arr[i];
//// //number.push_back(value);
//// }
//
// long long ans = mergeSort(arr, n);
// cout << endl;
// cout << ans;
// return 0;
//}
//
#include <bits/stdc++.h>
using namespace std;
long long count;
int array1[1000005];
int array2[1000005];
void mergesort(int *array, int len, int offset)
{
if (len > 1)
{
int *first = array1 + offset;
if (first == array)
first = array2 + offset;
for (int i = 0; i < len; ++i)
first[i] = array[i];
int firstLength = len / 2;
int secondStart = firstLength, secondLength = len - secondStart;
int *second = first + secondStart;
mergesort(first, firstLength, offset);
mergesort(second, secondLength, offset + secondStart);
int i = 0, j = 0;
int pos = 0;
int currentCost = count;
while (i < firstLength && j < secondLength)
{
if (first[i] < second[j])
array[pos++] = first[i++];
else
{
array[pos++] = second[j++];
count += firstLength - i;
}
}
for (; i < firstLength; ++i, ++pos)
array[pos] = first[i];
for (; j < secondLength; ++j, ++pos)
array[pos] = second[j];
}
}
int main()
{
int N;
while (cin >> N)
{
for (int i = 0; i < N; ++i)
cin >> array1[i];
mergesort(array1, N, 0);
cout << count << '\n';
count = 0;
}
}