题面:有A个红苹果,美味度分别为p[i];有B个青苹果,美味度分别为q[i];另外还有C个无色苹果,美味度分别为r[i],无色苹果在吃之前可以涂成红色或青色。现在要吃X个红苹果和Y个青苹果,求能吃到的最大美味度。
范围:1 <= X <= A <= 1E5; 1 <= Y <= B <= 1E5; 1 <= C <= 1E5; 1 <= p[i],q[i],r[i] <= 1E9
分析:反悔贪心,先不考虑无色苹果,按要求吃美味度最大的红苹果和青苹果,放到小根堆里,然后用美味度更大的无色苹果去替换堆里的苹果,使答案更优。
#include <bits/stdc++.h>
void solve() {
int X, Y, A, B, C;
std::cin >> X >> Y >> A >> B >> C;
std::vector<int> p(A), q(B), r(C);
for (int i = 0; i < A; i++) std::cin >> p[i];
for (int i = 0; i < B; i++) std::cin >> q[i];
for (int i = 0; i < C; i++) std::cin >> r[i];
std::sort(p.begin(), p.end(), std::greater());
std::sort(q.begin(), q.end(), std::greater());
std::priority_queue<int,std::vector<int>,std::greater<>> pq;
for (int i = 0; i < X; i++) pq.push(p[i]);
for (int i = 0; i < Y; i++) pq.push(q[i]);
for (int i = 0; i < C; i++) {
auto t = pq.top();
if (r[i] > t) {
pq.pop();
pq.push(r[i]);
}
}
int ans = 0;
while (!pq.empty()) {
ans += pq.top();
pq.pop();
}
std::cout << ans << "\n";
}
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int t = 1;
while (t--) solve();
return 0;
}
标签:反悔贪心