poj3614

来源:互联网 发布:谁人知我心可 编辑:程序博客网 时间:2024/06/09 15:44

Sunscreen
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9321 Accepted: 3261

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 23 102 51 56 24 1

Sample Output

2


思路:先进行排序,bottle和cow都按从小到大贪心地使用,先bottle从小到大,对于每个bottle,每种最小值满足的cow都压入优先队列,然后在选择使用的过程中,如果这种cow的最大值小于bottle提供的值,则直接出队列,对于可以使用的则进行使用,直到这种bottle用完或者队列未空,如果结束队列不为空,则队列中剩下的牛cow可以在下一轮中进行判断,能否使用下一种bottle。



#include<iostream>#include<stdio.h>#include<queue>#include<algorithm>using namespace std;typedef pair<int,int> P;priority_queue<int, vector<int>, greater<int> >q;      //从小到大的优先队列 int c,l;P cow[2505],bottle[2505];int main(){cin>>c>>l;for(int i=0;i<c;i++)cin>>cow[i].first>>cow[i].second;for(int i=0;i<l;i++)cin>>bottle[i].first>>bottle[i].second;sort(cow,cow+c);          //cow按最低值升序排序 sort(bottle,bottle+l);                                   //bottle按其值升序排序 int j=0,ans=0;                   //j代表cow序号 for(int i=0;i<l;i++){while(j<c&&cow[j].first<=bottle[i].first)          //把cow最低值小于bottle的值的cow最高值压入队列 {q.push(cow[j].second);j++;}int x;while(!q.empty()&&bottle[i].second)            //如果队列中 cow最高值大于等于bottle的值则可以给这头牛使用 {x=q.top();q.pop();if(x<bottle[i].first)continue;ans++;bottle[i].second--;}}cout<<ans<<endl;}




原创粉丝点击