-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalls.cpp
More file actions
52 lines (41 loc) · 1.15 KB
/
malls.cpp
File metadata and controls
52 lines (41 loc) · 1.15 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
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main() {
// uncomment the following lines if you want to read/write from files
// ifstream cin("input.txt");
// ofstream cout("output.txt");
long long N, K;
cin >> N >> K;
long long D[N];
for (int i = 0; i < N; i++) {
cin >> D[i];
}
// insert your code here
sort(D, D+N);
long long ans[2], temp, pos, d1, d2; // ans[0] = pos, ans[1] = dist
if (D[0] > K-D[N-1]){
ans[0] = 0;
ans[1] = D[0];
} else {
ans[0] = K;
ans[1] = K-D[N-1];
}
for (int i=0; i<N-1; i++) {
pos = (D[i] + D[i+1])/2;
d1 = min (abs(D[i]-pos), abs(D[i+1]-pos)); // distanza a sinistra
d2 = min (abs(D[i]-(pos+1)), abs(D[i+1]-(pos+1))); // distanza a destra
if (max(d1, d2) > ans[1]) {
if (d1 > d2) {
ans[0] = pos;
ans[1] = d1;
} else {
ans[0] = pos+1;
ans[1] = d2;
}
}
}
cout << ans[0] << endl; // change 42 with actual answer
return 0;
}