-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatedSortedArray.cpp
More file actions
196 lines (168 loc) · 4.76 KB
/
Copy pathcreatedSortedArray.cpp
File metadata and controls
196 lines (168 loc) · 4.76 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <bits/stdc++.h>
using namespace std;
int tree[4000040];
class Solution
{
public:
int get_mid(int a, int b)
{
return a + (b - a) / 2;
}
// Fn to get sum between range s to e
int query(int index, int s, int e, int qs, int qe)
{
// 1. Base Case - Complete Overlapp
if (s >= qs and e <= qe)
return tree[index];
// 2. Base Case - No overlapp
if (e < qs or s > qe)
return 0;
// 3. Partial Overlapp
int mid = get_mid(s, e);
int leftAns = query(2 * index + 1, s, mid, qs, qe);
int rightAns = query(2 * index + 2, mid + 1, e, qs, qe);
return leftAns + rightAns;
}
void update(int index, int s, int e, int pos)
{
// 1. Base Case - Reached the node where update is required
if (s == e)
{
tree[index]++;
return;
}
// 2. Intermidiate Node
int mid = get_mid(s, e);
if (pos <= mid)
update(2 * index + 1, s, mid, pos);
else
update(2 * index + 2, mid + 1, e, pos);
tree[index] = tree[2 * index + 1] + tree[2 * index + 2];
}
int createSortedArray(vector<int> &instructions)
{
int cost = 0;
const int MAXN = 1e5 + 1;
const int MOD = 1e9 + 7;
for (auto x : instructions)
{
// Get number of elements that are less than current element
int less_count = query(0, 0, MAXN, 0, x - 1);
// Get number of elements which are greater than current element
int greater_count = query(0, 0, MAXN, x + 1, MAXN);
update(0, 0, MAXN, x);
cost = (cost + min(less_count, greater_count)) % MOD;
}
return cost;
}
};
class SolutionNaive
{
public:
int createSortedArray(vector<int> &instructions)
{
int n = instructions.size();
multiset<int> ms;
int ans = 0;
for (int i : instructions)
{
auto low = ms.lower_bound(i);
auto high = ms.upper_bound(i);
int fromEnd = distance(high, ms.end());
int fromStart = distance(ms.begin(), low);
ans += min(fromStart, fromEnd);
ms.insert(i);
}
return ans;
}
};
class Solution2
{
public:
vector<int> seg;
int query(int index, int low, int high, int l, int r)
{
if (low >= l && high <= r)
{
return seg[index];
}
if (high < l || low > r)
return 0;
int mid = (low + high) / 2;
int left = query(2 * index + 1, low, mid, l, r);
int right = query(2 * index + 2, mid + 1, high, l, r);
return right + left;
}
void update(int index, int low, int high, int pos)
{
if (low == high)
{
seg[index]++;
return;
}
else
{
int mid = (low + high) / 2;
if (pos <= mid)
{
update(2 * index + 1, low, mid, pos);
}
else
{
update(2 * index + 2, mid + 1, high, pos);
}
seg[index] = seg[2 * index + 1] + seg[2 * index + 2];
}
}
int createSortedArray(vector<int> &instructions)
{
// using 100001 as 1e5 is the max element value
seg.resize(4 * 100001, 0);
int mod = 1e9 + 7;
int ans = 0;
for (int i = 0; i < instructions.size(); i++)
{
int mi = query(0, 0, 100001, 0, instructions[i] - 1); //number of elements smaller than instruction[i]
int mx = query(0, 0, 100001, instructions[i] + 1, 100001); // number of elements greater than instruction[i]
update(0, 0, 100001, instructions[i]);
ans = (ans + min(mi, mx)) % mod;
}
return ans;
/*TLE approch using STL
// 50/65 cases passed
int m=instructions.size();
vector<int>a;
int mod=1e9+7;
int ans=0;
int mxe=INT_MIN;
unordered_map<int,int>um;
for(auto x:instructions)
{
int xx=lower_bound(a.begin(),a.end(),x)-a.begin();
int n=a.size();
um[x]++;
mxe=max(mxe,x);
if(n==0)
{
a.push_back(x);
}
else
{
a.insert(a.begin()+xx,x);
int mi=xx;
int m=a.size();
ans=(ans+min(mi,m-mi-um[x]))%mod;
}
}
return ans;*/
}
};
int main()
{
int n;
cin >> n;
vector<int> instructions(n);
for (int &i : instructions)
cin >> i;
cout << Solution2().createSortedArray(instructions) << " ";
}