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?
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++编写:
这道题关键在于理解题干的意思,意思理解了就比较简单了。结合了一下其他翻译的好的各大博主的翻译,大致意思这样:
在海滩上晒太阳时,为了避免难看的灼伤而晒黑,有C头奶牛必须使用防晒霜,每头奶牛都有一个最小和最大的SPF等级(1≤ minSPFi≤1000; minSPFi≤ maxSPFi≤1000),如果SPF评级太低,奶牛会遭受晒伤; 如果SPF等级太高,奶牛根本不会晒黑(此处的意思就是说涂了防晒霜我们需要让奶牛得到的SPF在minSPFi~MAX_SPFi这个区间里面)
现在有L瓶防晒霜,每瓶有一个固定的SPF值,涂在奶牛身上那奶牛得到的SPF就是这个值,求最多可以让多少头牛晒太阳。
那么意思理解了就好办了,采用贪心+优先队列,代码如下:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int MAX_N=2510;
struct value{
int min,max; //SPF的最小值和最大值
}cows[MAX_N];
struct Screen{
int spf; //防晒霜的SPF值
int cover; //防晒霜的瓶数
}bottles[MAX_N];
int C,L;
bool compare1(const value&a,const value&b)
{
return a.min<b.min; //将奶牛按照SPF最小值从小到大的顺序排列
}
bool compare2(const Screen&a,const Screen &b)
{
return a.spf<b.spf; //将防嗮霜按照spf值从小到大排列
}
void solve()
{
for(int i=0;i<C;i++)
cin>>cows[i].min>>cows[i].max;
for(int i=0;i<L;i++)
cin>>bottles[i].spf>>bottles[i].cover;
sort(cows,cows+C,compare1);
sort(bottles,bottles+L,compare2);
priority_queue<int, vector<int>, greater<int> > que;
int sum=0,index=0; //sum:能够晒太阳牛的数量的值,index:可以涂防晒霜的牛的数量
for(int i=0;i<L;i++) //注意这里判断i的值是用L来比较判断
{
while(index<C && cows[index].min<=bottles[i].spf) //只要是SPF最小值比防晒霜SPF值小的,都先拿进去放着
{
que.push(cows[index].max);
index++;
}
int num;
while(!que.empty() && bottles[i].cover) //优先队列不为空并且该种瓶子数量不为0
{
num=que.top(); //从优先队列中取出SPF最小值小的奶牛的SPF最大值
que.pop();
if(num<bottles[i].spf)
continue; //如果防晒霜瓶子的SPF值比该牛SPF最大值还大,则不能用于这头牛
bottles[i].cover--;
sum++;
}
}
cout<<sum<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin>>C>>L;
solve();
return 0;
}