Sunscreen POJ - 3614
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 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 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 SPFi (1 ≤ SPFi ≤ 1,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?
Input
- Line 1: Two space-separated integers: C and L
- Lines 2…C+1: Line i describes cow i’s lotion requires with two integers: minSPFi and maxSPFi
- Lines C+2…C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri
Output
A single line with an integer that is the maximum number of cows that can be protected while tanning
Sample Input
3 2
3 10
2 5
1 5
6 2
4 1
Sample Output
2
题意:
有C头牛(Cow)可以接受的防晒值有最小值mi,最大值ma。
有L种防晒乳(lotion),每种的防晒乳防晒值num,有sum瓶。
问:最多有多少头牛可以不被晒伤。
题解:
把每头牛的可接受防晒值按照从小到大排序mi为第一优先级,ma为第二优先级。每瓶防晒乳也从小到大排序。
将每瓶防晒乳都用在最小mi可以满足的ma最小的情况下,即区间大小最小。
具体解释看代码。
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
using namespace std;
struct Cow {
int mi, ma;
};
struct lotion {
int num,sum;
};
Cow c[2510];
lotion b[2510];
bool cmp1(Cow a, Cow b) {
if(a.mi != b.mi) return a.mi < b.mi;
else a.ma < b.ma;
}
bool cmp2(lotion a, lotion b) {
return a.num < b.num;
}
int main() {
int C, L;
scanf("%d%d",&C, &L);
for (int i = 0; i < C ;i++) cin >> c[i].mi >> c[i].ma;
for (int i = 0; i < L ;i++) cin >> b[i].num >> b[i].sum;
sort (c, c+C, cmp1);
sort (b, b+L, cmp2);
int ans = 0;
priority_queue < int, vector<int>, greater<int> > q;
int cnt = 0;
//对于每种防晒乳
for (int i = 0; i < L ;i++) {
//把防晒乳可以起作用的都压入队列
while (cnt < C && c[cnt].mi <= b[i].num) {
q.push(c[cnt].ma);
cnt++;
}
//将防晒乳满足所有的满足条件的情况
for (; !q.empty() && b[i].sum != 0;q.pop()) {
int top = q.top();
if(top >= b[i].num) {
b[i].sum--;
ans++;
}
}
}
cout << ans << endl;
}