poj3614 Sunscreen【贪心】

Sunscreen

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11772   Accepted: 4143

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

题意:

有c头牛,每头牛有一个区间。有l种防晒霜,每种有一个spf值和对应的瓶数。牛只能用在他区间内的防晒霜。问最多能有多少牛被涂。

思路:

按minspf排序,每次取minspf最高的一头牛,给他安排可以被安排的spf值最高的防晒霜。

因为我们这样排了序之后,能给minspf高的牛用的防晒霜肯定也能给minspf低一些的牛用。

这时候就要尽量用spf值高的。

 1 #include <iostream>
 2 #include <set>
 3 #include <cmath>
 4 #include <stdio.h>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 //#include <bits/stdc++.h>
11 using namespace std;
12 typedef long long LL;
13 #define inf 0x7f7f7f7f
14
15 int c, l;
16 const int maxn = 2505;
17 struct lotion{
18     int spf;
19     int cover;
20 }lo[maxn];
21 struct mow{
22     int minspf, maxspf;
23 }cow[maxn];
24
25 bool cmpcow(mow a, mow b)
26 {
27     if(a.minspf == b.minspf){
28         return a.maxspf < b.maxspf;
29     }
30     return a.minspf < b.minspf;
31 }
32
33 bool cmplo(lotion a, lotion b)
34 {
35     return a.spf < b.spf;
36 }
37
38 int main()
39 {
40     while(scanf("%d%d", &c, &l) != EOF){
41         for(int i = 0; i < c; i++){
42             scanf("%d%d", &cow[i].minspf, &cow[i].maxspf);
43         }
44         //cout<<cow[0].minspf<<" "<<cow[0].maxspf<<endl;
45         for(int i = 0; i < l; i++){
46             scanf("%d%d", &lo[i].spf, &lo[i].cover);
47         }
48         sort(cow, cow + c, cmpcow);
49         //cout<<cow[0].minspf<<" "<<cow[0].maxspf<<endl;
50         sort(lo, lo + l, cmplo);
51
52         int cnt = 0;
53         for(int i = c - 1; i >= 0; i--){
54             for(int j = l - 1; j >= 0; j--){
55                 if(lo[j].spf < cow[i].minspf)break;
56                 if(lo[j].spf <= cow[i].maxspf && lo[j].cover > 0 && lo[j].spf >= cow[i].minspf){
57                     lo[j].cover--;
58                     cnt++;
59                     break;
60                 }
61             }
62         }
63         printf("%d\n", cnt);
64     }
65     return 0;
66 }

原文地址:https://www.cnblogs.com/wyboooo/p/9901449.html

时间: 2024-08-30 15:08:00

poj3614 Sunscreen【贪心】的相关文章

POJ3614 Sunscreen 贪心入门

题目大意 给出一些区间和一些点,一个点如果在一个区间内,那么此两者可以匹配.问匹配数最大是多少. 题解 这样的题我们一般都是站在区间上去找与其配对的点.我们可以得到如下性质: 对于一段区间\([l_1,r_1]\)的任意两点\(a,b, a<b\),它们对于任意一个区间\([l_2,r_2],l_2<l_1\),\(a\in[l_2,r_2]\)的可能性(以后用P表示)\(P(a\in[l_2,r_2])>P(b\in[l_2,r_2])\). 什么叫"可能性大"呢?

poj3614 Sunscreen 【优先队列】

题意 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值,太大就晒伤了,太小奶牛没感觉. 而刚开始的阳光的强度非常大,奶牛都承受不住,然后奶牛就得涂抹防晒霜,防晒霜的作用是让阳光照在身上的阳光强度固定为某个值. 那么为了不让奶牛烫伤,又不会没有效果. 给出了L种防晒霜.每种的数量和固定的阳光强度也给出来了 每个奶牛只能抹一瓶防晒霜,最后问能够享受晒太阳的奶牛有几个. 那么将奶牛按照阳光强度的最小值从小到大排序. 将防晒霜也按照能固定的阳光

POJ3614 Sunscreen 【贪心】

Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4204   Accepted: 1458 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

【POJ3614】【USACO 2007 Nov Gold】 3.Sunscreen 贪心

题意: 有若干个区间,若干种数,每个数告诉你有多少个. 然后一个数可以被放到一个x∈该区间 的区间,问最多有多少个区间可以被放. 题解: 显然我们可以用二分图最大匹配做,水题. 但是此题有别的技巧. 就是我们可以贪心进行处理. 首先我们考虑到需要将两种数都排个序. 然后再进行贪心. 一种错误的贪心法是单调队列式贪心,就是记录个top,然后单调往后推. 这个不仔细想还不知道它是错的. 额,至于卡它的数据,,我可以提供给你一个错误代码和一个拍子,你们可以自己拍一组数据出来,很高效. 这里贴一份错误代

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

http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固定阳光强度在某一个值,每种的数量是cover[i] ,每头奶牛只能用一瓶防晒霜,问最多有多少头奶牛能在沙滩上晒太阳. 理解题意之后还是挺好做的. 首先确定的贪心策略是,在满足min_spf的条件下,尽量用spf小的用在max_spf大的奶牛身上,用一个最小堆维护max_spf的最小值即可. 先对奶牛

Sunscreen [POJ3614] [贪心]

描述 C (1 ≤ C ≤ 2500) 头奶牛在海滩边晒太阳,要避免在日光浴时产生难看的灼伤,每头奶牛必须用防晒霜覆盖它的皮肤.第 i 头奶牛有一个最小和最大 SPF 值 (1 ≤ minSPFi ≤ 1,000; minSPFi ≤ maxSPFi ≤ 1,000) 将会起作用.如果 SPF 值太低,则奶牛会受到日光灼伤:如果 SPF 值太高,则牛奶无法进行日光浴. 奶牛们有一个野餐篮子,带了 L (1 ≤ L ≤ 2500) 瓶防晒霜乳液,第 i 瓶的 SPF 值是 SPFi (1 ≤ SP

POJ 3614 Sunscreen(贪心)

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

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

Language: Default Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4499   Accepted: 1565 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 th

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

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