题目:
题意:
有
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;
}