NC25025 [USACO 2007 Nov G]Sunscreen

题目

题目描述

To avoid unsightly burns while tanning, each of the \(C\) (\(1 ≤ C ≤ 2500\)) cows must cover her hide with sunscreen when they're at the beach. Cow \(i\) has a minimum and maximum \(SPF\) rating (\(1 ≤ minSPF_i ≤ 1,000; minSPF_i ≤ maxSPF_i ≤ 1,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\) (\(1 ≤ L ≤ 2500\)) bottles of sunscreen lotion, each bottle \(i\) with an \(SPF\) rating \(SPF_i\) (\(1 ≤ SPFi ≤ 1,000\)). Lotion bottle \(i\) can cover \(cover_i\) 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 \(2 \cdots C+1\) : Line \(i\) describes cow \(i\)'s lotion requires with two integers: \(minSPF_i\) and \(maxSPF_i\)
  • Lines \(C+2 \cdots C+L+1\) : Line \(i+C+1\) describes a sunscreen lotion bottle \(i\) with space-separated integers: \(SPF_i\) and \(cover_i\)

输出描述

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 @ 2022-06-15 20:03  空白菌  阅读(47)  评论(0)    收藏  举报