POJ-3614-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 ≤ 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?

输入:

  • 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

输出:

A single line with an integer that is the maximum number of cows that can be protected while tanning

输入示例:

1
2
3
4
5
6
3 2
3 10
2 5
1 5
6 2
4 1

输出示例:

1
2

题目大意:

有C头牛,每个牛Ci在一个给定的区间上需要涂防晒霜,有L种防晒霜,给出每个防晒霜适合的数值SPF[i]和个数cover[i],求能最多满足牛的个数。

贪心策略:

按区间的开始位置递减排序,依次考虑每头牛。
对于每头牛,选取满足在此区间内最大的防晒霜。

Solution1(暴力版):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<utility>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> PII;
const int MAXN = 5000+10;
int C,L;
PII p[MAXN];
PII q[MAXN];
int main(){
int ans = 0;
scanf("%d%d",&C,&L);
for(int i = 0; i < C; i++){
scanf("%d%d",&p[i].first,&p[i].second);
}
sort(p,p+C);
for(int i = 0; i < L; i++){
int a,b;
scanf("%d%d",&a,&b);
q[i].first = a;
q[i].second = b;
}
sort(q,q+L);
//从开始位置大到小考察每头牛。
for(int i = C-1; i >= 0; i--){
//同样,对于每头牛,考察在该牛区间内最右的防晒霜。
for(int j = L-1; j >= 0; j--){
if(q[j].second > 0 && q[j].first >= p[i].first && q[j].first <= p[i].second){
ans++;
q[j].second--;
break;
}
}
}
printf("%d",ans);
return 0;
}

Solution2(平衡树版):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int MAXN = 2510;
typedef pair<int,int> PII;
int N,M;
PII cows[MAXN];
int main(){
cin >> N >>M;
for(int i = 0; i < N; i++){
cin >> cows[i].first >> cows[i].second;
}
sort(cows,cows+N);
map<int,int> spfa;
for(int i = 0; i < M; i++){
int spa,cover;
cin >> spa >> cover;
spfa[spa] += cover;
}
spfa[0] = spfa[1001] = N;
int ans = 0;
for(int i = N-1; i >= 0; i--){
map<int,int>::iterator it = spfa.upper_bound(cows[i].second);
--it;
if(cows[i].first <= it->first && cows[i].second >= it->first){
ans++;
if(-- it->second == 0) spfa.erase(it);
}
}
cout << ans;
return 0;
}