-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbinary-soap-mystery.cpp
53 lines (46 loc) · 976 Bytes
/
binary-soap-mystery.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
const int N = 2e5 + 11;
void solve() {
int no_of_soaps;
cin >> no_of_soaps;
vector<int> prices(no_of_soaps);
for (int i = 0; i < no_of_soaps; i++)
cin >> prices[i];
sort(prices.begin(), prices.end());
int query;
cin >> query;
int less_then;
while (query--) {
cin >> less_then;
int lo = 0, hi = no_of_soaps - 1;
int mid = 0;
int result = -1;
while (lo <= hi) {
mid = (lo + hi) >> 1;
if (prices[mid] == less_then) {
result = mid;
hi = mid - 1;
} else if (prices[mid] < less_then)
lo = mid + 1;
else
hi = mid - 1;
}
cout << (result == -1 ? lo : result) << endl;
}
}
int main() {
freopen("input.txt", "r", stdin);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int no_of_test_cases = 1;
cin >> no_of_test_cases;
while (no_of_test_cases--)solve();
return 0;
}