poj 3614

来源:互联网 发布:全球经济数据时时播报 编辑:程序博客网 时间:2024/05/28 23:10

题意:给你第一行第一个数据是奶牛n,第二行防晒霜m。然后下面n行依次是每个奶牛最小防晒值和最大防晒值。后m行是防晒霜能够把阳光固定的值和数量。求有多少头牛可以晒太阳,也就是几瓶防晒霜管用了;

思路:首先先排除一些 比如牛的最小防晒值不能大于防晒霜的固定值 否则防晒霜失效  我们分别用两个结构体存 牛 和 防晒霜 用sort排序 按照最小防晒值和防晒霜固定值由小到大排序  依次排除上面所说的  符合条件的放进优先队列(注意时放进最大防晒值) 然后完了在比较 最大防晒值可不可以由防晒霜固定 具体见代码

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <queue>

#define MAXN 30000


using namespace std;


struct node

{

    int minn;

    int maxn;

    bool operator < (const node& x)const

    {

        return minn < x.minn;

    }

}cow[MAXN];

struct Node

{

    int value;

    int moment;

    bool operator < (const Node& x)const

    {

        return value < x.value;

    }


}bottle[MAXN];

int n,m;

priority_queue<int,vector<int>,greater<int> > p;

int main()

{

   scanf("%d%d",&n,&m);

   for(int i = 0; i < n; i ++)

   {

       scanf("%d%d",&cow[i].minn,&cow[i].maxn);

   }

   for(int i = 0; i < m; i ++)

   {

       scanf("%d%d",&bottle[i].value,&bottle[i].moment);

   }

   sort(cow,cow + n);

   sort(bottle,bottle + m);

   int t = 0;

   int ans = 0;

   for(int i = 0; i < m; i ++)

   {

       while(t < n && cow[t].minn <= bottle[i].value)

       {

           p.push(cow[t].maxn);

           t ++;

       }

       while(bottle[i].moment && !p.empty())

       {

           int temp = p.top();

           p.pop();

           if(temp < bottle[i].value) continue;

           bottle[i].moment --;

           ans ++;

       }

   }

   printf("%d\n",ans);

    return 0;

}



0 0