1. 程式人生 > >POJ 3614:Sunscreen 貪心+優先佇列

POJ 3614:Sunscreen 貪心+優先佇列

Sunscreen
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5378 Accepted: 1864

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頭牛,每頭牛有自己的承受陽光的最小值和最大值。現在有L瓶防晒霜,能夠保持,一定數額的陽光。。。。然後還有一定的數量。問最多有多少頭牛是被保護著的。

對牛進行排序,承受陽光小的放前面。

對防晒霜進行排序,保持陽光小的放在前面。

對每一種防晒液進行遍歷,然後把能塞進來的牛即牛的最小值<防晒霜的值,都塞到優先佇列中來。然後優先佇列往外面是按照牛的最大值的遞增順序,先往外面彈小的,然後往外面彈大的。這樣貪心就能夠保證如果成功是最優的結果,失敗了的話後面的防晒霜也不可能符合規格了。

程式碼:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#pragma warning(disable:4996)
using namespace std;

struct no1 {
	int min_v;
	int max_v;
}cow[2505],lotion[2505];

int num_c, num_l;

bool cmp2(no1 x, no1 y)
{
	if (x.min_v == y.min_v)
	{
		return x.max_v < y.max_v;
	}
	else
	{
		return x.min_v < y.min_v;
	}
}

class cmp
{
public:
	bool operator()(int x, int y)
	{
		return x > y;
	}
};

priority_queue<int, vector<int>, cmp>qu;

int main()
{
	int i,j,ans;
	scanf("%d%d", &num_c, &num_l);

	for (i = 1; i <= num_c; i++)
	{
		scanf("%d%d", &cow[i].min_v, &cow[i].max_v);
	}
	for (i = 1; i <= num_l; i++)
	{
		scanf("%d%d", &lotion[i].min_v, &lotion[i].max_v);
	}
	sort(cow + 1, cow + num_c + 1, cmp2);
	sort(lotion + 1, lotion + num_l + 1, cmp2);

	j = 1; 
	ans = 0;
	
	for (i = 1; i <= num_l;i++)
	{
		while (j <= num_c && cow[j].min_v <= lotion[i].min_v)
		{
			qu.push(cow[j].max_v);
			j++;
		}
		while (qu.size() != 0 && lotion[i].max_v != 0)
		{
			int x = qu.top();
			qu.pop();
			if (x < lotion[i].min_v)
			{
				continue;
			}
			else
			{
				ans++;
				lotion[i].max_v--;
			}
		}
	}
	cout << ans << endl;
	return 0;
}