Sunscreen POJ - 3614 -优先队列

题意:

输入第一行输入n,m,后n行,每行两个数子,然后m行,每行两个数子。

代表n头奶牛,两个数字:每头牛能 承受阳光的最小值和最大值;代表m种防晒霜,两个数字:防晒霜能固定奶牛承受的阳光,该种防晒霜的数量。

求能通过防晒霜固定享受阳光的牛最多几只?

思路:

奶牛,防晒霜分别用两个结构体类型存储,都从小到大排好序。然后将承受的阳光最小值小于防晒霜固定值的牛的承受阳光的最大值放入一个队列pq。优先队列先出最小值,然后将能承受阳光的最大值大于防晒霜固定的值的牛统计累加,注意防晒霜的数量。

AC代码:

#include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<vector>
#include <bitset>
#include<algorithm>
#include <queue>
#include<map>
using namespace std;
struct c
{ int mi,ma;
    c(int a=0,int b=0):mi(a),ma(b){}


}cow[3000];
struct f
{  int z,num;
    f(int a=0,int b=0):z(a),num(b){}


}s[3000];
bool cmp(c a, c b)
{
    return a.mi<b.mi;
}
bool cmp1(f a,f b)
{
    return a.z<b.z;
}
//typedef pair <int,int> P;
priority_queue<int,vector<int>,greater<int> > pq;
int main()
{
    int a,b;
    int ans=0;
    cin>>a>>b;
    for(int i=0;i<a;i++)
        cin>>cow[i].mi>>cow[i].ma;
    for(int j=0;j<b;j++)
        cin>>s[j].z>>s[j].num;
       sort(cow,cow+a,cmp);
       sort(s,s+b,cmp1);
        int k=0;
        for(int i=0;i<b;i++)
        {
            while(k<a&&cow[k].mi<=s[i].z)
            {

                pq.push(cow[k].ma);
                k++;
            }

        while(!pq.empty()&&s[i].num)
        {
            int o=pq.top();
            pq.pop();
            if(o<s[i].z)
                continue;
            ans++;
            s[i].num--;
        }

       }
        cout<<ans<<endl;

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值