题目十六 贪心算法+优先队列

来源:互联网 发布:苹果u盘安装mac系统 编辑:程序博客网 时间:2024/06/01 07:22
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct A
{
 int l;
 int r;
 bool operator< (A a) const{
  return r>a.r;
 };
}cow[2505];
struct B
{
 int f;
 int num;
}bot[2505];
bool cmpc(A a,A b)
{
 return a.l<b.l;
}
bool cmpb(B a,B b)
{
 return a.f<b.f;
}
int main()
{
 int C,L;
 while(~scanf("%d %d",&C,&L))
 {
  for(int i=1;i<=C;i++)
   scanf("%d %d",&cow[i].l,&cow[i].r);
  for(int i=1;i<=L;i++)
   scanf("%d %d",&bot[i].f,&bot[i].num); 
  sort(cow+1,cow+C+1,cmpc);
  sort(bot+1,bot+N+1,cmpb);
  int cnt=0,u=1;
  priority_queue<A> q;
  for(int i=1;i<=L;i++)
  {
   while(bot[i].f>=cow[u].l)
    q.push(cow[u++]);
   while(q.size()&&bot[i].num)
   {
    if(bot[i].f>q.top().r)
    {
     q.pop();
     continue;
    }
    cnt++;
    bot[i].num--;
    q.pop();
   }
  }
  printf("%d\n",cnt);
 }
 return 0;
}