poj -3614 Sunscreen(贪心 + 优先队列)

http://poj.org/problem?id=3614

有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固定阳光强度在某一个值,每种的数量是cover[i] ,每头奶牛只能用一瓶防晒霜,问最多有多少头奶牛能在沙滩上晒太阳。

理解题意之后还是挺好做的。

首先确定的贪心策略是,在满足min_spf的条件下,尽量用spf小的用在max_spf大的奶牛身上,用一个最小堆维护max_spf的最小值即可。

先对奶牛min_spf从小到大排序,在对防晒霜spf从小到大排序。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #pragma comment(linker, "/STACK:102400000,102400000")
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define E(x)        (1 << (x))
31 #define iabs(x)     (x) < 0 ? -(x) : (x)
32 #define OUT(x)  printf("%I64d\n", x)
33 #define lowbit(x)   (x)&(-x)
34 #define Read()  freopen("a.txt", "r", stdin)
35 #define Write() freopen("b.txt", "w", stdout);
36 #define maxn 1000000000
37 #define N 2510
38 #define mod 1000000000
39 using namespace std;
40
41 typedef pair<int,int>P;
42 P cow[N],bottle[N];
43
44 priority_queue<int, vector<int>, greater<int> >q;
45 int main()
46 {
47    // freopen("a.txt","r",stdin);
48     int c,l;
49     while(~scanf("%d%d",&c,&l)) {
50         for(int i=0;i<c;i++) {
51             scanf("%d%d",&cow[i].first,&cow[i].second);
52         }
53         for(int i=0;i<l;i++) {
54             scanf("%d%d",&bottle[i].first,&bottle[i].second);
55         }
56         sort(cow,cow+c);
57         sort(bottle,bottle+l);
58         int j=0,ans=0;
59         for(int i=0;i<l;i++)
60         {
61             while(j<c&&cow[j].first<=bottle[i].first)
62             {
63                 q.push(cow[j].second);
64                 j++;
65             }
66             while(!q.empty()&&bottle[i].second)
67             {
68                 int x=q.top();
69                 q.pop();
70                 if(x>=bottle[i].first)
71                 {
72                     ans++;
73                     bottle[i].second--;
74                 }
75             }
76         }
77         printf("%d\n",ans);
78     }
79     return 0;
80 }
时间: 2024-08-02 02:52:50

poj -3614 Sunscreen(贪心 + 优先队列)的相关文章

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

POJ 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;

poj 3614 Sunscreen 网络流或二分图多重匹配或优先队列

题意: 有C头牛,每头牛有它可以接受的药的最小值和最大值,有L瓶药,每瓶药有一个值u和它最多能给v头牛用,求最多有多少头牛能满足. 分析: 网络流或二分图多重匹配或优先队列,这道题优化了我的dinic模板,原来的模板会TLE... 代码: //poj 3614 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; const int maxN=5005;

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

poj 2431 Expedition 贪心+优先队列

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7707   Accepted: 2263 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv