POJ 3614 Sunscreen(贪心+优先队列)

Sunscreen

Time Limit: 1000MS


Memory Limit: 65536K

Total Submissions: 5713


Accepted: 1994

Description

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头奶牛要去沐光浴,太阳光太强烈会晒坏皮肤,太弱又会没效果。每头牛都有一个太阳光适宜的范围经行沐光浴,分别给出minspf_i和maxspf_i。  有L种防晒霜,每种防晒霜可以把所受阳光固定于一个值spf_i,每种有cover_i瓶。  问最多会有几头牛得到合适的光晒强度? 


题解:woc,USACO的牛生活真是丰富多彩啊(;′⌒`)   贪心策略,在满足minspf的条件下,尽量将spf的防晒霜涂到maxspf小的奶牛身上,因为maxspf大的奶牛有更多的选择。这里就需要一个优先队列来储存满足minspf的奶牛的maxspf的值。     具体解题步骤如下:


1.将奶牛按照minspf升序排列,将防晒霜按照spf升序排列。

2.枚举防晒霜,将minspf<=spf的奶牛的maxspf存到优先队列中,然后值小的先出队列,看是否满足maxspf>=spf,更新记录值。


代码如下:


#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
struct node1
{
	int minspf,maxspf;
}cow[2510];

struct node2
{
	int spf,num;
}lotion[2510];

int cmp1(node1 a,node1 b)
{
	return a.minspf<b.minspf;
}

int cmp2(node2 a,node2 b)
{
	return a.spf<b.spf;
}

int main()
{
	int n,m,i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		for(i=0;i<n;++i)
			scanf("%d%d",&cow[i].minspf,&cow[i].maxspf);
		for(i=0;i<m;++i)
			scanf("%d%d",&lotion[i].spf,&lotion[i].num);
		sort(cow,cow+n,cmp1);
		sort(lotion,lotion+m,cmp2);
		priority_queue<int, vector<int>,greater<int> >q;//维护被选中奶牛的maxspf 
		j=0;
		int ans=0;
		for(i=0;i<m;++i)
		{
			while(j<n&&cow[j].minspf<=lotion[i].spf)
			{
				q.push(cow[j].maxspf);
				j++;
			}
			while(!q.empty()&&lotion[i].num)
			{
				int cnt=q.top();
				q.pop();
				if(cnt>=lotion[i].spf)
				{
					ans++;
					lotion[i].num--;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}




你可能感兴趣的:(POJ 3614 Sunscreen(贪心+优先队列))