Sunscreen POJ - 3614 -优先队列

xiaoxiao2021-02-28  60

题意:

输入第一行输入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; }

转载请注明原文地址: https://www.6miu.com/read-54204.html

最新回复(0)