POJ No 3614 Sunscreen 优先队列 贪心

Sunscreen

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6410   Accepted: 2239

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <functional>
#include <utility>
using namespace std;

/*
3 2       // C只牛  L个防晒霜
C 行
3 10      minSPF, maxSPF
2 5
1 5
L行
6 2      每种数量, 固定的阳光强度
4 1
*/
const int maxn = 100000;
int C, L;                 // C只奶牛 , L
typedef pair<int, int> P; // minSPF, maxSPF
//小值先出
priority_queue<int, vector<int>, greater<int> > q;
P cow[maxn], bot[maxn];  //牛(min,max) 和 防晒霜(固定的阳光数, 数量) 

void solve();
void input();

void input()
{
    scanf("%d%d", &C, &L);
    for (int i = 0; i < C; i++) {
        scanf("%d%d", &cow[i].first, &cow[i].second);
    }

    for (int i = 0; i < L; i++) {
        scanf("%d%d", &bot[i].first, &bot[i].second);
    }
}

void solve()
{
    input();
    sort(cow, cow + C);    //按照最小值阳光强度升序
    sort(bot, bot + L);    //(first)按照能固定的阳光强度升序
    int j = 0, ans = 0;
    //当  L 个 防晒霜
    for (int i = 0; i < L; i++)
    {
        //将最小值阳光 和 固定的阳光强度 比较
        while (j < C && cow[j].first <= bot[i].first) {
            q.push(cow[j].second);    //添加到优先队列  (最小值阳光)
            j++;
        }        

        //如果 最小值阳光 序列不空,当前 防晒霜未用完
        while (!q.empty() && bot[i].second) {
            int x = q.top(); q.pop();
            // 最小值 小于  能固定的阳光, 方案不存在
            if (x < bot[i].first) continue;
            //否则,
            ans++;
            //当前防晒霜--
            bot[i].second--;
        }
    }
    printf("%d\n", ans);
}

int main()
{
    solve();

    return 0;
}
				
时间: 2024-08-24 22:07:33

POJ No 3614 Sunscreen 优先队列 贪心的相关文章

poj 3614 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 w

POJ 3614 Sunscreen 优先队列

http://poj.org/problem?id=3614 题目大意: 给你一些母牛,母牛有能容忍日光浴的最小和最大光照强度.每仅仅母牛能够涂一次SPF,SPF能够将母牛能够承受的光照强度固定在某个地方.如今给你母牛的最小和最大值和不同的spf的光照强度及其数量,求最多能够有多少母牛享受日光浴? 思路: 优先队列. 先按母牛最小承受的排好,然后spf的值也从小到大. 接下来用优先队列(栈顶为最小的).对于每一个spf,假设一仅仅母牛的最小值小于等于spf则将其最大值入队.(贪心..如两个母牛一

POJ 3614 Sunscreen(贪心)

POJ 3614 Sunscreen 题目链接 题意:转自http://blog.csdn.net/sdj222555/article/details/10698641 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值,太大就晒伤了,太小奶牛没感觉. 而刚开始的阳光的强度非常大,奶牛都承受不住,然后奶牛就得涂抹防晒霜,防晒霜的作用是让阳光照在身上的阳光强度固定为某个值. 那么为了不让奶牛烫伤,又不会没有效果. 给出了L种防晒霜.每种的数

【POJ 3614 Sunscreen】贪心 优先级队列

题目链接:http://poj.org/problem?id=3614 题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max]:有L瓶防晒液,每瓶有自己的spf值和容量(能供几头牛用). 求这L瓶防晒液最多能让多少头牛安全地晒太阳. 思路:贪心策略,按spf从小到大或从大到小的顺序取出防晒液,供给尽可能多的剩余的牛. 具体如何判断当前这瓶防晒液最多能供给几头牛呢? 以spf从小到大排序所有防晒液为例,可以维护一个小顶堆,每取出一瓶防晒液l,就把剩余的所有min值低于l.sp

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 2049 Finding Nemo 优先队列 STL

题目链接:http://poj.org/problem?id=2049 题目利用了<海底总动员>的情节,小丑鱼尼莫迷路了,他老爸去营救他便是题意. 题目给出了这样的地图,说是假设地图由墙和门组成,忽略墙的厚度,地图上有门,没有墙的地方是可以自由行动的问可以经过最少多少道门便可以营救到尼莫. 这个题给的数据是墙的交点为整数点,但鱼爸爸实在非墙的地方自由移动. 因此,这个题有两个难点: 1.如果建图保存地图 2.如何在地图上遍历 由于题目是给出一个点(x,y),来表示一段墙 我便用一对X,Y来表示

poj 2586 Y2K Accounting Bug (贪心)

Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9632   Accepted: 4806 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.

POJ 1442 Black Box(优先队列)

题目地址:POJ 1442 这题是用了两个优先队列,其中一个是较大优先,另一个是较小优先.让较大优先的队列保持k个.每次输出较大优先队列的队头. 每次取出一个数之后,都要先进行判断,如果这个数比较大优先的队列的队头要小,就让它加入这个队列,队列头移到较小优先的队列中.然后当较大优先的数不足k个的时候,就让较小优先的队列的队头移到较大优先的队头中. 代码如下: #include <iostream> #include <cstdio> #include <string>

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&