您的位置:首页 > 汽车 > 新车 > MT2094 甜品供应

MT2094 甜品供应

2024/7/2 4:27:15 来源:https://blog.csdn.net/l141930402/article/details/139623487  浏览:    关键词:MT2094 甜品供应

思路:

贪心策略:手下按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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com