C - お菓子の購入 / Buying Sweets Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
In this problem, we are given \(N\) types of snacks (each with a stock of \(A_i\) and a satisfaction value of \(B_i\) per piece), and we want to purchase exactly \(K\) snacks in total to maximize the sum of satisfaction. This can be solved by greedily prioritizing the snacks with higher satisfaction.
Analysis
Key Observation
For every snack, the satisfaction gained by purchasing one piece is constantly \(B_i\). When we have a quota of choosing a total of \(K\) snacks, the optimal way to maximize the total satisfaction is to prioritize snacks with a larger satisfaction \(B_i\) per piece. Therefore, a greedy approach works: we buy up snacks in descending order of satisfaction \(B_i\) until the stock \(A_i\) runs out (or until the total reaches \(K\)).
Naive Approach and Improvement
- Simulating picking one by one: If we consider choosing the snack with the highest satisfaction one piece at a time, up to \(10^9\) iterations would occur because \(K \le 10^9\), resulting in a TLE (Time Limit Exceeded).
- Purchasing in bulk: Instead of purchasing snacks of the same type one by one, we can speed this up by purchasing \(\min(K, A_i)\) pieces of snack \(i\) at once, which is the minimum of the remaining required count \(K\) and the snack’s stock \(A_i\). This reduces the number of iterations to at most \(N\), the number of snack types.
Edge Case (When \(K\) snacks cannot be bought)
If the total number of snacks is less than \(K\) even after purchasing all snacks up to their stock limits (\(\sum A_i < K\)), it is impossible to satisfy the condition, so output \(-1\).
Algorithm
- Store the information for each snack (satisfaction \(B_i\), stock \(A_i\)) as pairs in an array.
- Sort the array in descending order of satisfaction \(B_i\).
- Initialize the variable holding the answer (total satisfaction) to \(0\), and iterate through the sorted snacks from the beginning:
- Let the number of pieces to purchase from this snack be \(take = \min(K, A_i)\).
- Add \(take \times B_i\) to the answer.
- Subtract \(take\) from the remaining required count \(K\).
- If \(K = 0\), the target count has been reached, so terminate the loop.
- After the loop, if \(K > 0\) still holds, there are not enough snacks, so output \(-1\). If \(K = 0\), output the calculated total satisfaction.
Complexity
- Time Complexity: \(O(N \log N)\)
- Sorting the \(N\) snacks in descending order of satisfaction takes \(O(N \log N)\).
- The subsequent greedy loop runs at most \(N\) times, which takes \(O(N)\).
- Overall, it takes \(O(N \log N)\), which is well within the time limit under the constraint \(N \le 2 \times 10^5\).
- Space Complexity: \(O(N)\)
- It uses \(O(N)\) memory to store the information of the \(N\) types of snacks.
Implementation Notes
Beware of Integer Overflow: Since the number of snacks to purchase \(K\) can be up to \(10^9\) and the satisfaction \(B_i\) can also be up to \(10^9\), the total satisfaction can reach up to \(10^9 \times 10^9 = 10^{18}\). This significantly exceeds the maximum value of a standard 32-bit integer type (such as
intin C++, which is about \(2 \times 10^9\)). Therefore, make sure to use a 64-bit integer type (such aslong longin C++) for the variable storing the answer and for intermediate calculations.Implementation of Sorting: In C++, you can easily sort the snacks in descending order of satisfaction by storing
{satisfaction, stock}in apair<long long, long long>and callingsort(items.rbegin(), items.rend()).Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
long long k;
if (!(cin >> n >> k)) return 0;
// pair<B_i, A_i> で保持 (満足度, 在庫数)
vector<pair<long long, long long>> items(n);
for (int i = 0; i < n; ++i) {
cin >> items[i].second >> items[i].first;
}
// 満足度 B_i の降順にソート
sort(items.rbegin(), items.rend());
long long ans = 0;
for (int i = 0; i < n; ++i) {
long long take = min(k, items[i].second);
ans += take * items[i].first;
k -= take;
if (k == 0) break;
}
if (k > 0) {
cout << -1 << "\n";
} else {
cout << ans << "\n";
}
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: