-
Notifications
You must be signed in to change notification settings - Fork 0
/
2470.cpp
64 lines (55 loc) · 1.29 KB
/
2470.cpp
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
#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#define N 100005
using namespace std;
int n, arr[N], ans[3];
void find_ans(int x, int left, int right, int ban)
{
if (left + 1 >= right)
{
if (abs(x - arr[left]) > abs(x - arr[right]) && right != ban)
{
if (abs(x - arr[right]) < ans[0])
{
ans[0] = abs(x - arr[right]);
ans[1] = -x;
ans[2] = arr[right];
}
}
else
{
if (abs(x - arr[left]) < ans[0] && left != ban)
{
ans[0] = abs(x - arr[left]);
ans[1] = -x;
ans[2] = arr[left];
}
}
return;
}
int mid = (left + right) / 2;
if (arr[mid] < x)
find_ans(x, mid + 1, right, ban);
else
find_ans(x, left, mid, ban);
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int i;
cin >> n;
for (i = 1; i <= n; i++)
cin >> arr[i];
sort(arr + 1, arr + n + 1);
ans[0] = INT_MAX;
for (i = 1; i <= n; i++)
find_ans(-arr[i], 1, n, i);
if (ans[1] < ans[2])
cout << ans[1] << " " << ans[2];
else
cout << ans[2] << " " << ans[1];
return 0;
}