-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
72 lines (55 loc) · 1.84 KB
/
main.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
66
67
68
69
70
71
72
class Solution {
const int mod = 1e9 + 7;
public:
int maxProfit(vector<int>& inventory, int orders) {
unordered_map<int, int> cnt;
for (int v : inventory) ++cnt[v];
vector<pair<int, int>> balls(cnt.begin(), cnt.end());
balls.push_back({0, 0});
sort(balls.begin(), balls.end());
long long result = 0;
while (orders) {
int n = balls.size();
auto [r, c] = balls[n - 1];
int l = balls[n - 2].first;
if (orders >= 1LL * (r - l) * c) {
orders -= 1LL * (r - l) * c;
result += 1LL * (r + l + 1) * (r - l) * c / 2 % mod;
result %= mod;
balls.pop_back();
balls.back().second += c;
continue;
}
int v = orders / c;
result += 1LL * (r + r - v + 1) * v * c / 2 % mod;
result += 1LL * (r - v) * (orders % c) % mod;
result %= mod;
break;
}
return result;
}
};
class Solution {
typedef long long ll;
const int mod = 1e9 + 7;
public:
int maxProfit(vector<int>& inventory, int orders) {
inventory.push_back(0);
sort(inventory.begin(), inventory.end());
ll result = 0;
for (int i = inventory.size() - 1, colors = 1; i > 0; --i, ++colors) {
int hi = inventory[i], lo = inventory[i - 1];
if (hi == lo) continue;
int rounds = min(orders / colors, hi - lo);
orders -= rounds * colors;
result += ((ll)hi + hi - rounds + 1) * rounds * colors / 2 % mod;
result %= mod;
if (rounds < hi - lo) {
result += ((ll)hi - rounds) * orders % mod;
result %= mod;
break;
}
}
return result;
}
};