-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11057.cpp
51 lines (43 loc) · 1.15 KB
/
11057.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
#include "bits/stdc++.h"
using namespace std;
int compareints (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main(int argc, char const *argv[]) {
int n, m;
while (scanf("%d", &n) != EOF) {
int books [n];
for (int i = 0; i < n; ++i){
int price;
scanf("%d",&price);
books[i] = price;
}
sort (books, books + n);
scanf("%d",&m);
cin.ignore();
map <int, int> buy;
for (int i = 0; i < (n); ++i) {
int temp = m - books[i];
int *found = (int*) bsearch (&temp, books, n, sizeof (int), compareints);
if (found && found != (books + i)) {
buy[books[i]] = temp;
}
else if (found == (books + i) && found == (books + i + 1)) {
buy[books[i]] = temp;
}
}
map<int,int>::iterator it = buy.begin();
int dif = (1<<29);
int a = 0, b = 0;
for (it = buy.begin(); it != buy.end(); ++it) {
if (abs (it->first - it -> second) < dif) {
a = it->first;
b = it->second;
dif = abs (it->first - it -> second);
}
}
printf("Peter should buy books whose prices are %d and %d.\n\n",a,b);
}
return 0;
}