NC25025 [USACO 2007 Nov G]Sunscreen

题目

题目描述

To avoid unsightly burns while tanning, each of the C (1C2500) cows must cover her hide with sunscreen when they're at the beach. Cow i has a minimum and maximum SPF rating (1minSPFi1,000;minSPFimaxSPFi1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........

The cows have a picnic basket with L (1L2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi (1SPFi1,000). Lotion bottle i can cover coveri 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 1 : Two space-separated integers: C and L
  • Lines 2C+1 : Line i describes cow i's lotion requires with two integers: minSPFi and maxSPFi
  • Lines C+2C+L+1 : Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

输出描述

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

题解

知识点:枚举,贪心。

我们希望使用防晒的牛是最优选择,即在自己有独立机会时不占用别的牛的可用机会。因此,考虑建立一个能够选择机会少的序列能够优先选择的排序。对于一系列由同一同侧端点的区间,选择最靠近另一端点的防晒是最优的,因为这样使得对小区间的机会影响最小化。再者,通过对这个同一端点从小到大排序,对于每一组同一同侧端点如上操作,就能每次使得选择最优。

我们选择对区间右端点从小到大排序,因此要选择最靠左的可行防晒,于是防晒因从小到大排序来选择。对每右端点对应一系列区间,从小到大选择防晒,使得防晒最靠左。

时间复杂度 O(cl)

空间复杂度 O(c+l)

代码

#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;
}
posted @   空白菌  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示