JZOJ 1246. 挑剔的美食家【平衡树】


题目:

传送门


题意:

n n n头牛和 m m m中牧草,每头牛只会吃比自己要求的更贵的、更鲜的的牧草,每头牛吃的草都不相同
问要满足所有牛所要的最小代价是多少


分析:

因为有两个限制,所以我们先以鲜嫩值排序,然后因为要求满足比花费大而又要尽可能的省钱,所以我们把花费放入平衡树里进行维护


代码:

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<vector>
#include<set>
#define LL long long
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
multiset<int> mu;
struct node{
	LL x,y;
}a[100005],b[100005];
bool cmp(node x,node y) {return x.y>y.y;}
int main()
{
	LL n=read(),m=read();
	for(LL i=1;i<=n;i++) a[i].x=read(),a[i].y=read(); 
	for(LL i=1;i<=m;i++) b[i].x=read(),b[i].y=read();
	sort(a+1,a+1+n,cmp);sort(b+1,b+1+m,cmp);
	LL l=1;
	multiset<int>::iterator it;
	LL ans=0;
	for(LL i=1;i<=n;i++)
	{
		while(a[i].y<=b[l].y&&l<=m) {mu.insert(b[l].x);l++;}
		it=mu.lower_bound(a[i].x);
		if(it==mu.end()) return !printf("-1");
		ans+=(*it);
		mu.erase(it);
	}
	cout<<ans;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值