ABC308F 题解
题意:有 个物品和 张优惠券,第 张优惠券可以在金额 的物品上使用,可以将该物品的加格减小 ,求最小花费。
盲猜贪心。
我们先将所有的原价存进 std::multiset 里,并将所有的优惠券以 为第一关键字降序, 为第二关键字升序排序,然后枚举,使用 lower_bound 找到可以用这个优惠券的最小价格,然后将它移出 std::multiset,直到优惠券全部被遍历完成。
#include<iostream>
#include<algorithm>
#include<numeric>
#include<set>
const int sz=2e5+10;
using ll=long long;
std::multiset<ll>p;
struct coupon{
int l,d;
bool operator<(const coupon&a)const{
if(d!=a.d)return d>a.d;
return l<a.l;
}
}c[sz];
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n,m;
std::cin>>n>>m;
for(int i=1,x;i<=n;i++)std::cin>>x,p.insert(x);
for(int i=1;i<=m;i++)std::cin>>c[i].l;
for(int i=1;i<=m;i++)std::cin>>c[i].d;
std::sort(c+1,c+m+1);
ll tot=std::accumulate(p.begin(),p.end(),0ll);
for(int i=1;i<=m;i++){
int l=c[i].l,d=c[i].d;
auto it=p.lower_bound(l);
if(it!=p.end())p.erase(it),tot-=d;
}
std::cout<<tot<<"\n";
return 0;
}