题解:P17302 [ICPC 2026 Xi'an I] VIP Coupon
认为没有青,最多绿。
我的证明可能说的差一些,可能好理解一点(
\text{Solution}
看到题目就能想到维护购买优先队列和优惠券优先队列,令两个队头(目前代价最大和优惠最大)的物品、优惠券做比较。
如果这样更优则购买此券,如果不优也不能立刻原价购买,因为优惠小一点的购买价格低一些也有可能优惠到。
接着不难发现该做法每个优惠券只对代价最大物品进行一次判断,如何证明这个优惠券只需要对代价最大的物品进行一次判断即可?
注意到当前该券一定是优惠最大的,该物品也是消耗最大的,考虑两种情况:
- 该券优惠后商品价格变为负数,从而变成
0 ,少优惠了一些,但是由于该物品消耗最大,故这是最小化少优惠价格的方法。 - 该券优惠后商品价格仍为正数,也就是说,只要不亏,你为何不现在买?若不在该物品购买,在其他物品购买有可能令价格变为负数,故当前买最优。
还要注意一点,优惠券是可以优惠买优惠券的,所以使用优惠券优惠时要把购买的优惠券加入队列,因为还要考虑优惠,所以只需要令当前物品的优惠后的价格累加即可。
处理完所有的优惠券后,所有购买队列的物品均只能原价购买,同样进行累加,最后输出所有的累加结果。
\text{Accepted Code}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5;
struct Queue {
int b, c;
friend bool operator < (const Queue &l, const Queue &r) { return l.c < r.c; }
};
inline void solve() {
int n, m, a[N], b[N], c[N], ans = 0;
priority_queue<int> p; priority_queue<Queue> q;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i], p.push(a[i]);
for (int i = 1; i <= m; i++)
cin >> b[i];
for (int i = 1; i <= m; i++)
cin >> c[i], q.push({b[i], c[i]});
while (!q.empty()) {
auto [w, v] = q.top(); q.pop(); int k = p.top();
int t = max(k - v, 0ll) + w;
if (t < k)
ans += max(k - v, 0ll), p.pop(), p.push(w);
}
while (!p.empty())
ans += p.top(), p.pop();
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int T;
cin >> T;
while (T--)
solve();
return 0;
}