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

Sunscreen
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4478   Accepted: 1557

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

首先 这道题要想对 贪心的思路 如果当前这些牛 可以使用这个防晒霜 那么一定选择r最小的使用 因为r大的后面有更多的选择

基于此 将牛和防晒霜都按升序排列  用防晒霜去匹配牛 如果当前防晒霜不能涂抹该牛 那么此牛一定不能使用其他任何防晒霜

- - 因为升序排列 后面的更大  然后把满足条件的都push到pq里 然后 每次取最小的就好了 

AC代码如下:

//
//  POJ 3614 Sunscreen
//
//  Created by TaoSama on 2015-03-13
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))

using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
pair<int, int> cow[2505], b[2505];

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(cin >> n >> m) {
		for(int i = 1; i <= n; ++i) cin >> cow[i].first >> cow[i].second;
		for(int i = 1; i <= m; ++i) cin >> b[i].first >> b[i].second;
		sort(cow + 1, cow + 1 + n); sort(b + 1, b + 1 + m);

		priority_queue<int, vector<int>, greater<int> > pq;
		int ans = 0, j = 1;
		for(int i = 1; i <= m; ++i) {
			while(j <= n && cow[j].first <= b[i].first) {
				pq.push(cow[j].second);
				++j;
			}
			while(!pq.empty() && b[i].second) {
				int MinSPF = pq.top(); pq.pop();
				if(MinSPF >= b[i].first)
					++ans, --b[i].second;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


你可能感兴趣的:(poj,优先队列,贪心)