poj3614 priority queue

http://poj.org/problem?id=3614


Sunscreen
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5293   Accepted: 1841

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
奶牛在沙滩晒太阳,为了不被晒伤,每一头奶牛头给予涂抹防晒霜(sunscreen),每头牛需要涂抹的防晒霜FPS有一个最大值,一个最小值,(FPS,防晒霜级数),有L个篮子,每个篮子里的防晒霜FPS相同,每个篮子里有n瓶防晒霜,每瓶防晒霜只能给一头牛使用,求最多有多少头牛可以很好地去沙滩晒太阳;;;;
#include <iostream>
#include <queue>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
struct node
{
    int frist,second;
};
node cow[2505],sec[2505];
int C,L;
int cmp(node a,node b)
{
    return a.frist<b.frist;
}
priority_queue<int ,vector<int>,greater<int> >que;///优先队列,从小到大排序
int main()
{
    while(scanf("%d%d",&C,&L)!=-1)
    {
        for(int i=0; i<C; i++)
            scanf("%d%d",&cow[i].frist,&cow[i].second);
        for(int i=0; i<L; i++)
            scanf("%d%d",&sec[i].frist,&sec[i].second);
        sort(cow,cow+C,cmp);///按照奶牛FPS最小值从小到大排序
        sort(sec,sec+L,cmp);///按照防晒霜FPS从小到大排序;
        int k=0,ans=0;
        for(int i=0; i<L; i++)///以防晒霜顺序查找
        {
            while(k<C&&cow[k].frist<=sec[i].frist)///如果该奶牛min FPS小于 第i个防晒霜FPS
            {
                que.push(cow[k].second);///将该奶牛max FPS加入队列
                k++;
            }
            while(!que.empty()&&sec[i].second)
            {
                int t=que.top();
                que.pop();
                if(t<sec[i].frist)///如果最小的  奶牛 max-FPS小于该防晒霜  就继续
                    continue;
                ans++;
                sec[i].second--;
            }
        }
        printf("%d\n",ans);

    }
    return 0;
}


你可能感兴趣的:(poj3614 priority queue)