-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11057.cpp
More file actions
37 lines (35 loc) · 923 Bytes
/
11057.cpp
File metadata and controls
37 lines (35 loc) · 923 Bytes
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
#include <bits/stdc++.h>
int main() {
int a[10001];
int i, j, n, t, m, path, max, min;
while (scanf("%d", &t) == 1) {
for (i = 0; i < t; i++) scanf("%d", &a[i]);
scanf("%d", &m);
std::sort(a, a + t);
min = i = 0;
max = j = t - 1;
path = a[j] - a[i];
while (1) {
while (a[i] + a[j] > m) {
j--;
}
while (a[i] + a[j] < m) {
i++;
}
if (a[i] + a[j] == m && (a[j] - a[i] <= path) && i != j) {
max = j;
min = i;
path = a[j] - a[i];
i++;
}
if (j <= i) break;
}
if (max < min) {
i = max;
max = min;
min = i;
}
printf("Peter should buy books whose prices are %d and %d.\n\n", a[min], a[max]);
}
return 0;
}