Sunscreen - poj3614 - 贪心、优先队列

Sunscreen

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10937   Accepted: 3835

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

Source

USACO 2007 November Gold

思路:

把牛的数组,minSPF值从小到大排,相等情况下,maxSPF值大的优先,然后,把防晒霜的值也按从小到大排序。

  1. 对于每一瓶防晒,把minSPF小于等于防晒SPF的牛的maxSPF值push进优先队列,对于这瓶防晒,只能在push进的这些牛里选(没push进的,其minSPF大于防晒霜SPF不满足牛的要求)
  2. 对于每一个在队列中的牛,我们优先选择maxSPF小的,(但是这个最小值也要大于等于该防晒霜的SPF值)因为大的有更多的选择机会,这个防晒霜处理完有两种情况(1、队列的牛小于防晒霜数,该队列所有牛都满足,防晒霜剩余。2、防晒霜不够用,队列还剩牛,这时候处理下一个防晒霜,下一个防晒霜的spf肯定比这个防晒霜大,因此在队列的牛都满足minSPF<=nextSPF)

其实我也证明不出来这个思想就一定正确,但是在没别的方法下,不如用贪心思想,因为它真的hin神奇。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue> 
 
using namespace std;
typedef pair<int,int> P;
P cow[2505],sun[2505];

int main(){
	int c,l;
	scanf("%d%d",&c,&l);
	
	for(int i=0;i<c;i++){
		scanf("%d%d",&cow[i].first,&cow[i].second);
	}
	for(int i=0;i<l;i++){
		scanf("%d%d",&sun[i].first,&sun[i].second);
	}
	sort(cow,cow+c);
	sort(sun,sun+l);
	priority_queue<int,vector<int>,greater<int> > qq;
	int j=0,ans=0;
	for(int i=0;i<l;i++){
		while(j<c&&cow[j].first<=sun[i].first){
			qq.push(cow[j].second);
			j++; 
		} 
		while(!qq.empty()&&sun[i].second){
			int mymin=qq.top();
			qq.pop(); 
			if(mymin>=sun[i].first){
				ans++;
				sun[i].second--;
			}
		}
	}
	printf("%d\n",ans);
}

猜你喜欢

转载自blog.csdn.net/m0_37579232/article/details/81366219