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

· · 题解

题目传送门 ::::info[没看过题目的看这里] 你要买 n 个纪念品,每个纪念品有对应的价格 a_i,还可以使用 m 张优惠券,每张优惠券要 b_j 元,可以降低下一个物品 c_j 元。

注意物品价格不会变成负数, 每个物品(纪念品和优惠券)最多可以使用一张优惠卷,求最小总代价。 ::::

思路

考虑贪心。

对于每张优惠券,我们希望它能优惠尽量多的价格,因此,我们要尽可能的去让 b_i 大的优惠券去优惠价格大的物品。

并且如果 b_j \ge c_j 或者匹配之后价格无法更优,那么我们就没必要选它。

因此我们可以先将优惠券按 b_j 排序,从大到小遍历优惠券,每次对价格最大的物品用这张优惠券。

::::info[代码]

#include<bits/stdc++.h>
using namespace std;
struct node{
    int c,b;
};

bool cmp(node x,node y)
{
    return x.c>y.c;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long ans=0;
        int n,m;
        cin>>n>>m;
        priority_queue<long long> q;
        for(int i=1;i<=n;i++)
        {
            int a;
            cin>>a;
            q.push(a);
        }
        node k[m+1];
        for(int i=1;i<=m;i++)
        {
            cin>>k[i].b;
        }
        for(int i=1;i<=m;i++)
        {
            cin>>k[i].c;
        }
        sort(k+1,k+m+1,cmp);
        for(int i=1;i<=m;i++)
        {
            if(k[i].b>=k[i].c)
                continue; 
            int a=q.top();
            if(max(a-k[i].c,0)+k[i].b<a)
            {
                ans+=max(a-k[i].c,0);
                q.pop();
                q.push(k[i].b);
            }
        }
        while(!q.empty())
        {
            ans+=q.top();
            q.pop();
        }
        cout<<ans<<endl;
    }
    return 0;
}

::::