NC25025 [USACO 2007 Nov G]Sunscreen
题目
题目描述
To avoid unsightly burns while tanning, each of the () cows must cover her hide with sunscreen when they're at the beach. Cow has a minimum and maximum rating () that will work. If the rating is too low, the cow suffers sunburn; if the rating is too high, the cow doesn't tan at all........
The cows have a picnic basket with () bottles of sunscreen lotion, each bottle with an rating (). Lotion bottle can cover cows with lotion. A cow may lotion from only one bottle.
What is the maximum number of cows that can protect themselves while tanning given the available lotions?
输入描述
- Line : Two space-separated integers: and
- Lines : Line describes cow 's lotion requires with two integers: and
- Lines : Line describes a sunscreen lotion bottle with space-separated integers: and
输出描述
A single line with an integer that is the maximum number of cows that can be protected while tanning.
示例1
输入
3 2 3 10 2 5 1 5 6 2 4 1
输出
2
题解
知识点:枚举,贪心。
我们希望使用防晒的牛是最优选择,即在自己有独立机会时不占用别的牛的可用机会。因此,考虑建立一个能够选择机会少的序列能够优先选择的排序。对于一系列由同一同侧端点的区间,选择最靠近另一端点的防晒是最优的,因为这样使得对小区间的机会影响最小化。再者,通过对这个同一端点从小到大排序,对于每一组同一同侧端点如上操作,就能每次使得选择最优。
我们选择对区间右端点从小到大排序,因此要选择最靠左的可行防晒,于是防晒因从小到大排序来选择。对每右端点对应一系列区间,从小到大选择防晒,使得防晒最靠左。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> using namespace std; pair<int, int> a[2507], b[2507]; int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int c, l; cin >> c >> l; for (int i = 0;i < c;i++) cin >> a[i].first >> a[i].second; for (int i = 0;i < l;i++) cin >> b[i].first >> b[i].second; sort(a, a + c, [&](pair<int, int> a, pair<int, int> b) {return a.second < b.second;}); sort(b, b + l); int cnt = 0; for (int i = 0;i < c;i++) { for (int j = 0;j < l;j++) { if (b[j].second && a[i].first <= b[j].first && b[j].first <= a[i].second) { b[j].second--; cnt++; break; } } } cout << cnt << '\n'; return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16379661.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧