-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva11057.cpp
More file actions
58 lines (53 loc) · 1.3 KB
/
uva11057.cpp
File metadata and controls
58 lines (53 loc) · 1.3 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
53
54
55
56
57
58
/* Rishikesh
* UVA 11057 Exact Sum
*/
#include<iostream>
#include<string>
#include<sstream>
#include<iomanip>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<iomanip>
#include<cstdint>
#include<numeric>
#include<stack>
#include<queue>
#include<list>
#include<deque>
#include<bitset>
using namespace std;
int main() {
string s;
while(getline(cin, s), s.length()) {
int n, budget;
n = stoi(s);
vector<int> prices(n);
istringstream iss;
getline(cin, s); iss = istringstream(s);
for(int i = 0; i < n; i++) {
iss >> prices[i];
}
getline(cin, s); iss = istringstream(s);
iss >> budget;
sort(prices.begin(), prices.end());
int first = 0, last = prices.size() -1;
int book1, book2;
while(first < last) {
int currsum = prices[first] + prices[last];
if (currsum < budget)
first++;
else if (currsum > budget)
last--;
else {
book1 = prices[first];
book2 = prices[last];
first++; last--;
}
}
cout << "Peter should buy books whose prices are " << book1
<< " and " <<book2 << ".\n\n";
getline(cin, s);
}
}