POJ 3614(Sunscreen)

题目链接:http://poj.org/problem?id=3614

题意:

有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值,太大就晒伤了,太小奶牛没感觉。

给出了L种防晒霜及每种每种防晒霜的防晒指数,每个奶牛只能抹一瓶防晒霜,最后问能够享受晒太阳的奶牛有几个。

Output: A single line with an integer that is the maximum number of cows that can be protected while tanning.

思路:

应该是一道优先队列的题,下列给出两种解答形式:

1. 不用优先队列:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define MAX_N 2505
using namespace std;
typedef long long ll;
int C,L;

struct node
{
	int minspf;
	int maxspf;
}cow[MAX_N];

struct sums
{
	int spf;
	int num;
}sun[MAX_N];

bool cmp(node m,node n){
	return m.maxspf==n.maxspf?m.minspf<n.minspf:m.maxspf<n.maxspf;
}

bool cmp2(sums m,sums n){
	return m.spf<n.spf;
}

int main(void){
	while(~scanf("%d%d",&C,&L)){
		for(int i=0;i<C;i++)
			scanf("%d%d",&cow[i].minspf,&cow[i].maxspf);
		for (int i=0;i<L;++i)
			scanf("%d%d",&sun[i].spf,&sun[i].num);
		sort(cow,cow+C,cmp);
		sort(sun,sun+L,cmp2);

		int ans=0;
		for(int i=0;i<C;i++){
			for(int j=0;j<L;j++){
				if(sun[j].spf>=cow[i].minspf&&sun[j].spf<=cow[i].maxspf&&sun[j].num>0)
				{
					ans++;
					sun[j].num--;
					break;
				}
			}
		}
		printf("%d
",ans);
	}
	
	return 0;
}

  

2. 用优先队列(代码源于网络)

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#define MAXN 2555
#define INF 1000000007
using namespace std;
int C, L;
typedef pair<int, int> P;
priority_queue<int, vector<int>, greater<int> > q;
P cow[MAXN], bot[MAXN];
int main()
{
    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", &bot[i].first, &bot[i].second);
    sort(cow, cow + C);
    sort(bot, bot + L);
    int j = 0, ans = 0;
    for(int i = 0; i < L; i++)
    {
        while(j < C && cow[j].first <= bot[i].first)
        {
            q.push(cow[j].second);
            j++;
        }
        while(!q.empty() && bot[i].second)
        {
            int x = q.top();
            q.pop();
            if(x < bot[i].first) continue;
            ans++;
            bot[i].second--;
        }
    }
    printf("%d
", ans);
    return 0;
}
// 原文链接:https://blog.csdn.net/sdj222555/article/details/10698641

  

原文地址:https://www.cnblogs.com/jaszzz/p/12687053.html