-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sum of Two Values
54 lines (50 loc) · 1.14 KB
/
Sum of Two Values
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
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
pair<ll, ll> binarySearch(vector<pair<ll, ll>> &arr, ll in)
{
int left = 0;
int right = arr.size() - 1;
while (left < right) {
ll total = arr[left].first + arr[right].first;
if (total == in) {
return make_pair(left, right);
}
else if (in < total) {
right--;
}
else {
left++;
}
}
return make_pair(-1,-1);
}
int main()
{
int n;
ll x;
ll a;
cin >> n >> x;
vector<pair<ll, ll>> arr;
for (int i = 0; i < n; ++i) {
cin >> a;
arr.emplace_back(make_pair(a, i+1));
}
sort(arr.begin(), arr.end());
pair<ll, ll> fd = binarySearch(arr ,x);
if (fd.first == -1) {
cout << "IMPOSSIBLE";
}
else {
if (arr[fd.first].second < arr[fd.second].second) {
cout << arr[fd.first].second << " " << arr[fd.second].second << endl;
}
else {
cout << arr[fd.second].second << " " << arr[fd.first].second << endl;
}
}
return 0;
}