My ranking in the Cruel group has stayed at 14th. It seems this is where my level has converged. Recently, because I have been busy writing my master’s thesis, and on the other hand autumn recruitment has ended, I have not even done check-in problems. Only in November did I resume doing the daily problem on the US and Chinese servers to earn points for clothes. As for the Cruel group, because I can basically exempt myself from check-in, I have not checked in there for a month.
5561. Get Maximum in Generated Array
Warm-up problem. Fill in each element according to the recurrence formula.
classSolution { public: intminDeletions(string s){ vector<int> cnt(26, 0); for (char c : s) { ++cnt[c - 'a']; } set<int> count; int ans = 0; for (int a : cnt) { if (a != 0) { while (a != 0 && count.find(a) != count.end()) { ++ans; --a; } count.insert(a); } } return ans; } };
Time complexity: O(N), space complexity: O(26).
5563. Sell Diminishing-Valued Colored Balls
Greedy. Each time, try to take from the color with the most balls first. During the taking process, you do not need to take one by one; you can take as many as possible at once. Use a priority queue to maintain the maximum color count, and use an arithmetic progression sum to compute the cost of taking balls.
classSolution { using ll = longlong; const ll MOD = 1e9 + 7; using pii = pair<int, int>; public: intmaxProfit(vector<int>& inventory, int o){ ll orders = o; // greedy ll ans = 0; map<ll, ll, greater<ll>> cnt; for (ll i : inventory) { ++cnt[i]; } cnt[0] = 0; while (orders > 0) { auto first = *cnt.begin(); if (first.first == 0) return-1; auto second = *next(cnt.begin()); cnt.erase(cnt.begin()); cnt.erase(cnt.begin()); ll balls = (first.first - second.first) * first.second; pii newSecond = {second.first, second.second + first.second}; ll reduce = min(orders, balls); orders -= reduce; // cout << first.first << " " << first.second << " " << reduce << endl; const ll batch = reduce / first.second; const ll remain = reduce % first.second; ans = (ans + (first.first * batch - batch*(batch-1) / 2) * first.second) % MOD; ans = (ans + (first.first - batch) * remain) % MOD; // for (int i = 0; i * first.second < reduce; ++i) { // // cout << min(first.second, reduce - i) << " " << (first.first - i) << endl; // ans = (ans + min(first.second, reduce - i * first.second) * (first.first - i)) % MOD; // } // cout << ans << endl; cnt.insert(newSecond); } return ans; } };
Time complexity: O(N log N), N = inventory.size() space complexity: O(N).
The time complexity is not that obvious. You can consider the fact that the size of cnt decreases by one each time, so the while loop is executed at most N times.
5564. Create Sorted Array through Instructions
The core is that we need a data structure that can count the number of elements in a certain range. We can use a segment tree, Binary Indexed Tree (BIT), rank tree, and so on. During the contest, I first used a rank tree template implemented with GNU pbds, but it timed out. In theory, the complexity should have been fine. Then, because the previous day’s daily problem on the Chinese server also used the same data structure, I copied a treap template. Because I was unfamiliar with the interface, debugging alone took half an hour. Fortunately, I ACed in the last 5 minutes, so I did not need to check in with the Cruel group this week again. I had already skipped it for a month. Note that in the template interface, rank starts from index 1.