-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva11057.cpp
82 lines (60 loc) · 1.64 KB
/
uva11057.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <limits.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
#include <bitset>
using namespace std;
typedef pair<int, int> ii;
#define traverse(c, it) \
for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
int N, sum;
vector<int> prices;
ii findBestPair() {
vector<ii> pairs;
for(int i = 0; i < prices.size(); i++) {
vector<int>::iterator next = lower_bound(prices.begin() + i, prices.end(),
sum - prices[i]);
if(next != prices.end() && prices[i] + *next == sum) {
pairs.push_back(make_pair(i, next - prices.begin()));
}
}
int index = 0;
int minDiff = INT_MAX;
for(int i = 0; i < pairs.size(); i++) {
ii current = pairs[i];
if(prices[current.second] - prices[current.first] < minDiff) {
index = i;
minDiff = prices[current.second] - prices[current.first];
}
}
return pairs[index];
}
int main() {
bool first = true;
while(true) {
cin >> N;
if(cin.eof()) {
break;
}
prices.clear();
prices.resize(N);
for(int i = 0; i < N; i++) {
cin >> prices[i];
}
cin >> sum;
sort(prices.begin(), prices.end());
ii res = findBestPair();
//cout << prices[res.first] << " " << prices[res.second] << endl;
printf("Peter should buy books whose prices are %d and %d.\n",
prices[res.first], prices[res.second]);
cout << endl;
}
return 0;
}