Sunscreen


Time Limit: 1000MS


Memory Limit: 65536K

Total Submissions: 5538


Accepted: 1920


Description


To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with sunscreen when they're at the beach. Cow i has a minimum and maximum SPF rating (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) that will work. If the SPF rating is too low, the cow suffers sunburn; if the SPF rating is too high, the cow doesn't tan at all........

The cows have a picnic basket with L (1 ≤ L ≤ 2500) bottles of sunscreen lotion, each bottle i with an SPF rating SPFi(1 ≤ SPFi ≤ 1,000). Lotion bottle i can cover coveri cows with lotion. A cow may lotion from only one bottle.

What is the maximum number of cows that can protect themselves while tanning given the available lotions?


Input


* Line 1: Two space-separated integers: C and L
* Lines 2..C+1: Line i describes cow i's lotion requires with two integers: minSPFi and maxSPFi 
* Lines C+2..C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri


Output


A single line with an integer that is the maximum number of cows that can be protected while tanning


Sample Input

3 2
3 10
2 5
1 5
6 2
4 1

Sample Output

2

Source


USACO 2007 November Gold


非常好的一道题,刚开始一直不知道怎么贪心,,


思路如下:1.先将奶牛按l值(适合的阳光最小值)从小到大排序,护肤品按防晒能力从小到大排序;


                    2.对每瓶护肤品枚举适合它的奶牛,枚举很有技巧性, 构建一个优先队列,压入能够使用它的奶牛,可能会问万一一头奶牛适合多瓶护肤品怎么办?该使用哪一个?别急,首先将该奶牛在当前护肤品i的情况下压入该优先队列(能够用到护肤品的最基本条件),如果在当前i护肤品的情况下不能压入,那么i+1也显然不能压入,因为护肤品从小到大排序,所以只能等到下一个i来时看可否压入。
     3.,该优先队列维护r的最小值,因为r越大可以适用的护肤品越多(贪心),对压入优先队列的奶牛,如果其r值大于i的值,那么就说明该头奶牛可以使用该护肤品,则立即使用,因为越到后面r值越大,如果它能使用则后面的也肯定可以使用,但是随着i的值的增大,该母牛不能使用的可能性增加,所以应尽快使用(贪心)。并将该奶牛弹出(使用完),如果不能使用,则其r值<i值,也弹出,因为i只会变大,不会变小。


#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,n;
    while(~scanf("%d %d",&c,&n))
    {
        for(int i=1;i<=c;i++)
           scanf("%d %d",&cow[i].l,&cow[i].r);
        for(int i=1;i<=n;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<=n;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;
}