POJ3614_Sunscreen_贪心 && stl的优先级队列

来源:互联网 发布:wow7.0装备数据库 编辑:程序博客网 时间:2024/06/07 15:47

Sunscreen
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8256 Accepted: 2921

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

大致题意:

给牛涂防晒霜。防晒霜有不同地等级,相同等级地防晒霜有若干瓶,每瓶只能给一头牛用。每头牛能适应不同地防晒等级范围,在给出的最小值和最大值之间。给出几种防晒霜的等级和各自的瓶数,已经若干头牛能适应的等级范围,求最多能给多少牛涂上防晒霜。


大体思路:

贪心问题。弱遇到了好几个非常相似的问题,感觉这比单纯的贪心还稍微复杂一点。

总体思路就是,对于都能用上某一种防晒霜的牛,优先涂给适应上限小的牛。

1)把防晒霜按照等级由低到高排序,把牛按照适应下限由小到大排序。

2)创建一个牛的优先队列。队列中的牛按照适应上限由小到大排序。

3)枚举每一种防晒霜,做三件事:

把所有适应下限小于该种防晒霜等级的牛入队。

把当前队列中适应上限小于该种防晒霜等级的牛出队。

此时队列中剩下的牛的适应范围都包含该种防晒霜的等级。只要从前往后(适应上限从小到大)依次给牛涂(把牛出队),直到防晒霜用完或队列为空。

4)枚举下一种防晒霜


#include<iostream>#include<cstdio>#include<queue>#include<algorithm>using namespace std;#define rep(i,a,b) for(int i=a; i<=b; i++)#define mp(x,y) make_pair(x,y);typedef pair<int,int> p;struct cmp{bool operator () (const p& x, const p& y) const{return x.second > y.second;}};int C, L;p c [2510];p b [2510];int main (){//freopen("in.txt", "r", stdin);scanf("%d %d", &C, &L);rep(i, 0, C-1)scanf("%d %d", &c[i].first, &c[i].second);sort(c, c+C);rep(i, 0, L-1)scanf("%d %d", &b[i].first, &b[i].second);sort(b, b+L);priority_queue<p, vector<p>, cmp> qu;int i = 0, j = 0, cnt = 0;while(j < L){while(i<C && c[i].first<=b[j].first){qu.push(c[i] );i++;}while(qu.size() && qu.top().second<b[j].first)qu.pop();for(int k=0; qu.size() && k<b[j].second; k++){cnt++;qu.pop();}j++;}printf("%d\n",cnt);return 0;}


0 0
原创粉丝点击