「杂题乱刷」AT_abc308_f

链接(at)

链接(luogu)

简单贪心。

又是一道出过很多次的板子题。

容易发现,我们可以将商品价格先从小到大排序,然后使用指针维护,将所有能取的优惠价格放入单调队列里,然后取最大值,因为后面的商品同样也能取此商品去过的同样优惠,故而取最大值即可,不会对最终答案产生影响。

代码:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) x&-x
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
ll a[1000010],n,m,ans,l=1;
priority_queue<ll>q;
struct node{
	ll x,y;
}b[1000010];
bool cmp(node x,node y){
	return x.x<y.x;
}
int main()
{
	IOS;
	cin>>n>>m;
	forl(i,1,n)
		cin>>a[i];
	forl(i,1,m)
		cin>>b[i].x;
	forl(i,1,m)
		cin>>b[i].y;
	sort(a+1,a+1+n),sort(b+1,b+1+m,cmp);
	forl(i,1,n)
	{
		while(l<=m && b[l].x<=a[i])
			q.push(b[l].y),l++;
		ans+=a[i];
		if(!q.empty())
			ans-=q.top(),q.pop();
	}
	cout<<ans;
	QwQ;
}

posted @ 2024-01-26 22:43  wangmarui  阅读(3)  评论(0编辑  收藏  举报