forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minimum-relative-loss-after-buying-chocolates.cpp
37 lines (35 loc) · 1.33 KB
/
minimum-relative-loss-after-buying-chocolates.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
// Time: O((n + q) * logn)
// Space: O(n)
// sort, binary search, greedy, prefix sum
class Solution {
public:
vector<long long> minimumRelativeLosses(vector<int>& prices, vector<vector<int>>& queries) {
const auto& binary_search = [&](int left, int right, const auto& check) {
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
};
sort(begin(prices), end(prices));
vector<long long> prefix(size(prices) + 1);
for (int i = 0; i < size(prices); ++i) {
prefix[i + 1] = prefix[i] + prices[i];
}
vector<long long> result(size(queries));
for (int i = 0; i < size(queries); ++i) {
const auto& k = queries[i][0], &m = queries[i][1];
const long long cnt = binary_search(0, m - 1, [&](int x) {
return k - (prices[size(prices) - (m - x)] - k) <= prices[(x + 1) - 1] - 0;
});
const auto& a = prefix[size(prices)] - prefix[size(prices) - (m - cnt)] - (m - cnt) * k;
const auto& b = prefix[cnt] + (m - cnt) * k;
result[i] = b - a;
}
return result;
}
};