-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhorse_racing_duals.cc
81 lines (72 loc) · 1.8 KB
/
horse_racing_duals.cc
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
/**
* Horse-racing Duals
* https://www.codingame.com/ide/puzzle/horse-racing-duals
**/
int merge(vector<int>& h,int p,int q,int r) {
vector<int> lowerHalf(q + 1 - p);
vector<int> upperHalf(r - q);
int k = 0;
int j = 0;
for (int i = p; i <= r; i++) {
if (i <= q) {
lowerHalf[k] = h[i];
k++;
} else {
upperHalf[j] = h[i];
j++;
}
}
k = 0;
j = 0;
for (int i = p; i <= r; i++) {
if ((int)(k) < (int)(lowerHalf.size()) && (int)(j) < (int)(upperHalf.size())) {
if (lowerHalf[k] < upperHalf[j]) {
h[i] = lowerHalf[k];
k++;
} else {
h[i] = upperHalf[j];
j++;
}
} else {
if (k >= lowerHalf.size()) {
h[i] = upperHalf[j];
j++;
} else {
h[i] = lowerHalf[k];
k++;
}
}
}
}
int divide(vector<int>& h, int p,int r) {
if (p < r) {
int q = floor((p + r) / 2);
divide(h,p,q);
divide(h,q+1,r);
merge(h,p,q,r);
}
}
int main()
{
int N;
int lowest = 10000000;
cin >> N; cin.ignore();
vector<int> h(N);
for (int i = 0; i < N; i++) {
int Pi;
cin >> Pi; cin.ignore();
h[i] = Pi;
}
divide(h,0,(int)h.size() - 1);
for (int i = 0; i < h.size(); i++) {
int size = h.size() - 1;
if ((int)(i + 1) > (int)(size)) {
break;
} else if (abs(h[i] - h[i + 1]) < lowest) {
lowest = abs(h[i] - h[i + 1]);
}
}
// // Write an action using cout. DON'T FORGET THE "<< endl"
// // To debug: cerr << "Debug messages..." << endl;
cout << lowest << endl;
}