-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIce_Cream_Parlor.cpp
65 lines (55 loc) · 1.56 KB
/
Ice_Cream_Parlor.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class IceCream {
public:
int flavor;
int index;
IceCream(int flavor, int index) {
this->flavor = flavor;
this->index = index;
}
bool operator < (const IceCream& ic2) {
return (this->flavor < ic2.flavor);
}
};
int binarySearch(int first, int last, vector<IceCream> arr, int search) {
//if(first == last) return first;
while (first <= last) {
int mid = (first + last) / 2;
if (search == arr[mid].flavor) return arr[mid].index;
else if(search > arr[mid].flavor) first = mid + 1;
else last = mid - 1;
}
return -1;
}
int main() {
int t;
int n, m;
cin >> t;
for(int test = 0; test < t; test++) {
cin >> m >> n;
vector<IceCream> arr;
arr.reserve(n);
for (int i = 0; i < n; i++) {
int cost;
cin >> cost;
arr.push_back(IceCream(cost, i + 1));
}
sort(arr.begin(), arr.end());
int firstIndex = 100000, secondIndex = 100000;
for(int i = 0; i < n - 1 ; i++) {
int search = m - arr[i].flavor;
if(search >= arr[i].flavor) {
int index = binarySearch( i + 1, n - 1, arr, search);
if( index != -1 ) {
cout << min(arr[i].index, index) << " " << max(arr[i].index, index) << endl;
break;
}
}
}
}
}