【poj3614】Sunscreen

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
 
/*
    枚举+优先队列
  给奶牛的minSPF从小到大排序 防晒霜从小到大排序 从最小的防晒霜枚举 把minSPF小于防晒霜的牛的maxSPF加到优先队列当中 优先队列最小值优先,取出最小值。(因为较大的maxSPF有较大的选择空间) 更新答案
*/ #include<iostream> #include<cstdio> #include<algorithm> #include<queue> using namespace std; priority_queue<int> q; struct cownode { int minSPF,maxSPF; }cow[2510];// int sun[1000000];//防晒霜 int comp(const cownode & a,const cownode & b) { return (a.minSPF<b.minSPF); } int main() { int C,L,SPF,cover,ans=0,j; cin>>C>>L; for (int i=1;i<=C;i++) cin>>cow[i].minSPF>>cow[i].maxSPF; int sunsum=1; for (int i=1;i<=L;i++) { cin>>SPF>>cover; for (int j=sunsum;j<=sunsum+cover;j++) sun[j]=SPF; sunsum+=cover; } sunsum--; sort(cow+1,cow+C+1,comp); sort(sun+1,sun+sunsum+1); for (int i=1;i<=sunsum;i++) //枚举防晒霜 { for (j=1;j<=C;j++)//枚举牛 if(cow[j].minSPF<=sun[i]) { q.push(-cow[j].maxSPF); cow[j].minSPF=10000000;//给个极大值 再也不会进队列了 } int t=0; if (!q.empty()) t=-q.top();//注意是-的q.top(); while (t<sun[i] && !q.empty()) //如果最小的防晒霜也比maxSPF大 这头牛肯定不能晒太阳啦 { q.pop();//不能在↑取出q.top的时候就删除,当没用的时候再删除! if (!q.empty()) t=-q.top(); } if (t>=sun[i] && t!=0) { ans++; q.pop();//别忘了删除 } } cout<<ans<<endl; }

 

你可能感兴趣的:(【poj3614】Sunscreen)