题解
超市
2 条题解
-
0
P2313 超市
解题思路
按过期时间从早到晚处理商品,用小根堆维护当前保留的商品利润。处理期限为d的商品后,如果保留数量超过d,删除利润最小的一件;其余商品可以安排在期限内出售。输入直到EOF,每组独立计算,N=0输出0。
参考代码
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { vector<pair<int,int>> goods(n); for (auto &g:goods) cin >> g.second >> g.first; sort(goods.begin(), goods.end()); // deadline, profit priority_queue<int,vector<int>,greater<int>> chosen; long long answer=0; for (auto [deadline,profit]:goods) { chosen.push(profit); answer+=profit; if ((int)chosen.size()>deadline) { answer-=chosen.top(); chosen.pop(); } } cout << answer << '\n'; } }复杂度分析
每组时间复杂度O(N log N),空间复杂度O(N)。
-
0
P2313 超市
解题思路
按过期时间从早到晚处理商品,用小根堆维护当前保留的商品利润。处理期限为d的商品后,如果保留数量超过d,删除利润最小的一件;其余商品可以安排在期限内出售。输入直到EOF,每组独立计算,N=0输出0。
参考代码
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { vector<pair<int,int>> goods(n); for (auto &g:goods) cin >> g.second >> g.first; sort(goods.begin(), goods.end()); // deadline, profit priority_queue<int,vector<int>,greater<int>> chosen; long long answer=0; for (auto [deadline,profit]:goods) { chosen.push(profit); answer+=profit; if ((int)chosen.size()>deadline) { answer-=chosen.top(); chosen.pop(); } } cout << answer << '\n'; } }复杂度分析
每组时间复杂度O(N log N),空间复杂度O(N)。
- 1