思路:
贪心策略:手下按r进行从小到大排序。用小根堆存储甜品。如果堆顶甜品无法被当前手下满足,则将甜品放回堆中,看下一个手下能不能满足。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
struct people
{
int l, r;
bool operator<(const people &a) const
{
if (r == a.r)
{
return l < a.l;
}
return r < a.r;
}
} p[N];
struct sweet
{
int v;
int num;
bool operator>(const sweet &a) const
{
return v > a.v; // 使用greater,小根堆重载>号
}
};
int c, l, cnt[N], ans;
priority_queue<sweet, vector<sweet>, greater<sweet>> q, temp;
int main()
{
cin >> c >> l;
for (int i = 1; i <= c; i++)
{
cin >> p[i].l >> p[i].r;
}
sort(p + 1, p + c + 1); // 按区间从左到右排序
for (int i = 1; i <= l; i++)
{
sweet a;
cin >> a.v >> a.num;
cnt[a.v] += a.num;
q.push(a);
}
for (int i = 1; i <= c; i++)
{
while (!q.empty() && q.top().v < p[i].l)
{
temp.push(q.top());
q.pop();
}
if (!q.empty() && q.top().v <= p[i].r)
{
ans++;
cnt[q.top().v]--;
if (cnt[q.top().v] == 0)
{
q.pop();
}
}
while (!temp.empty())
{
q.push(temp.top());
temp.pop();
}
}
cout << ans;
return 0;
}