poj 3614 奶牛美容问题 优先队列

题意:每头奶牛需要涂抹防晒霜,其中有效的范围 min~max ,现在有L种防晒霜,每种防晒霜的指数为 f 瓶数为 l,问多少只奶牛可以涂上合适的防晒霜?
思路: 优先队列+贪心

  1. 当奶牛的 min<f 时 把奶牛的max 进队列
  2. 取队列 与 f比较 如果小于 继续 否则 l减去1 ans+1

思路解释:min<f时表示可以访问, 取max与f进行比较,因为先前对f进行排序,所以数小的在前面,这样可以做到不浪费

解决问题的代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int,int> p;
p cow[2555],bot[2555];
priority_queue<int, vector<int>, greater<int> >q;
int C,L;

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\n",ans);
    return 0;
}

 

posted @ 2018-08-02 13:10  徐小晋  阅读(105)  评论(0编辑  收藏  举报