题解:P17302 [ICPC 2026 Xi'an I] VIP Coupon

· · 题解

对于第 i 张优惠券,如果将它使用在一个价格为 v 的物品上,那么其实就只花费 \max(v - c_i, 0)。

所以考虑这样的贪心:c_i 越大的优惠券,应当用在 v 最大的物品上,因为优惠券最多只能省 c_i,如果用在便宜物品上,可能 v < c_i,这样显然是不划算的。

因此我们将所有纪念品压入一个大根堆,按照 c_i 从大到小对每一张优惠券排序,然后遍历每一张优惠券。

当遍历到第 i 张优惠券,堆顶为第 j 个物品时:

最后已经没有优惠券了,把堆中所有剩下的物品全部加一起即可。

#include<bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
struct Node{
    int b, c;
}x[N];
priority_queue<int> q;
int a[N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T; cin >> T;
    while (T--){
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; i++) cin >> a[i], q.push(a[i]);
        for (int i = 1; i <= m; i++) cin >> x[i].b;
        for (int i = 1; i <= m; i++) cin >> x[i].c;
        sort(x + 1, x + m + 1, [&](Node a, Node b){
            return a.c > b.c;
        });
        long long ans = 0;
        for (int i = 1; i <= m; i++){
            int v = q.top();
            if (x[i].b + max(v - x[i].c, 0) >= v) continue;
            else{
                ans += max(v - x[i].c, 0);
                q.pop();
                q.push(x[i].b);
            }
        }
        while (!q.empty()){
            ans += q.top();
            q.pop();
        }
        cout << ans << '\n';
    }
    return 0;
}