Sunscreen POJ - 3614(贪心)
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
- 题意:有C头牛,每个牛都有对阳光的承受空间【minspf,maxspf】,有L种防晒霜,每种都有SPF值和瓶数,涂到牛身上可使其只收到SPF值的阳光,每瓶只可涂一头牛,问最多几只牛可以享受阳光?
- 思路:
此题可以看成在一个数轴上操作。
图中矩形块就代表了牛的承受区间,我们从左到右对牛进行涂防晒霜处理,可以从图中看到,
①【0,1】位置,无牛需要防晒霜
②【1,4】位置,只有一头牛有需求,那我们直接给橙色涂
④【4,5】位置,红色牛和橙色牛都有需求,给哪只牛涂呢?
有人可能说了,前面都给橙色牛涂了,现在肯定给红色啦,对,但是如果在【4,5】之前没有防晒霜呢(就一瓶防晒霜在【4,5】位置),也就是说这瓶防晒霜面临两个选择,
我们从图中看出,那肯定也是给红色牛啦,因为红色牛快承受不住啦(该牛承受区间右边界小),也就是说再往下进行的话,马上就出了红牛的承受区间了,然而橙牛却还没有,
所以我们先给红牛,之后再瞅瞅有没有适合橙牛的。
这样就可以看出我们的策略:
①将牛按右边界从小到大排序,防晒霜按spf从小到大排序
②将牛按左边界从大到小排序,防晒霜按spf从大到小排序
回头看这问题,对于排好序的牛(假设按①方式),有两瓶防晒霜x>y,那么是选x的,因为x,y对于后面的牛来说有三种情况,
①x、y可选
②x不可选,y可选
③x、y不可选(由左边界影响)
那么我们选择小的就对后面的牛来说影响最小
并且因为选择了对后面牛最小的影响方式,该牛可以选择防晒霜,就没有必要留给下头牛,因为只算牛头数,哪头牛不是牛呢
- #include<cstdio>
- #include<algorithm>
- #include<iostream>
- using namespace std;
- int C,L;
- struct Node
- {
- int l;
- int r;
- } cow[],sun[];
- bool cmp1(Node a,Node b)
- {
- return a.l < b.l;
- }
- bool cmp2(Node a,Node b)
- {
- return a.r < b.r;
- }
- int main()
- {
- scanf("%d%d",&C,&L);
- for(int i=; i<=C; i++)
- {
- scanf("%d%d",&cow[i].l,&cow[i].r);
- }
- for(int i=; i<=L; i++)
- {
- scanf("%d%d",&sun[i].l,&sun[i].r);
- }
- sort(cow+,cow++C,cmp2);
- sort(sun+,sun++L,cmp1);
- int ans = ;
- for(int i=; i<=C; i++)
- {
- for(int j=; j<=L; j++)
- {
- if(cow[i].l <= sun[j].l && cow[i].r >= sun[j].l && sun[j].r)
- {
- ans++;
- sun[j].r--;
- break;
- }
- }
- }
- printf("%d\n",ans);
- }
优先队列写法:
- #include<bits/stdc++.h>
- using namespace std;
- int C,L;
- struct Node
- {
- int l;
- int r;
- bool operator<(Node b)
- {
- return l < b.l;
- }
- } cow[],sun[];
- priority_queue<int,vector<int>,greater<int> >que;
- int main()
- {
- scanf("%d%d",&C,&L);
- for(int i=; i<=C; i++)
- scanf("%d%d",&cow[i].l,&cow[i].r);
- for(int i=; i<=L; i++)
- scanf("%d%d",&sun[i].l,&sun[i].r);
- sort(cow+,cow++C);
- sort(sun+,sun++L);
- int j = ;
- int ans = ;
- for(int i=; i<=L; i++)
- {
- while(j<=C&&cow[j].l <= sun[i].l)
- que.push(cow[j].r),j++;
- while(!que.empty() && sun[i].r)
- {
- int tmp = que.top();
- que.pop();
- if(tmp >= sun[i].l)
- ans++,sun[i].r--;
- }
- }
- printf("%d\n",ans);
- }
Sunscreen POJ - 3614(贪心)的更多相关文章
- Heap:Sunscreen(POJ 3614)
晒太阳 题目大意:一堆牛,为了避免晒太阳会灼烧自己,然后他们自己有自己的防晒指数(一个区间),防晒霜可以提高防晒因数SPF,大了不行小了不行,现在有一桶防晒霜,他们提供一定的SPF,但是最多可以提供k ...
- POJ 3614 Sunscreen 贪心
题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...
- poj -3614 Sunscreen(贪心 + 优先队列)
http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...
- 【POJ 3614 Sunscreen】贪心 优先级队列
题目链接:http://poj.org/problem?id=3614 题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max]:有L瓶防晒液,每瓶有自己的spf值和容量(能供几头 ...
- POJ 3614:Sunscreen 贪心+优先队列
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5378 Accepted: 1864 Descrip ...
- POJ 3614 Sunscreen(贪心,区间单点匹配)
把牛的SPF看作一个区间,防晒霜看作点.一个点可以匹配C[i]次,问最大匹配数.可以用图论做. 也可以贪心.贪心的思想是,把区间和点排序以后,考虑最左边的点,加入和这个点相交的区间, 并排除出界的区间 ...
- POJ 3614 Sunscreen 优先队列 贪心
题意 有C个奶牛去晒太阳 (1 <=C <= 2500),每个奶牛各自能够忍受的阳光强度有一个最小值和一个最大值,太大就晒伤了,太小奶牛没感觉. 而刚开始的阳光的强度非常大,奶牛都承受不住 ...
- poj 3614 Sunscreen
...
- 【POJ 3614】 Sunscreen
[题目链接] http://poj.org/problem?id=3614 [算法] 将MinSPF从大到小排序,每头牛找SPF值最大的防晒霜 [代码] #include <algorithm& ...
随机推荐
- Confluence 6 管理协同编辑 - 代理和 SSL 的考虑
对于你如何连接 Synchrony 是与你的环境有关的.我们知道绝大部分的 Confluence 站点是运行在反向代理后面的,同时还使用了 SSL.这里是帮助你在你环境中识别正确的配置的一些信息和一 ...
- Confluence 6 在 Apache 或者系统级别阻止垃圾
如果一个垃圾发布机器人攻击你的 Confluence 站点,这些程序可能来自于同一个 IP 地址,或者是一个比较小范围的 IP 地址段.希望找到攻击者的 IP 地址,请参考 Apache access ...
- Confluence 6 反向跟踪
当反向跟踪(Trackback )被启用后,在任何你链接到可用启用自动发现功能的外部页面中,Confluence 将会自动发送一个方向跟踪 ping,这个 ping 能通知链接的页面有了内容改变. C ...
- Confluence 6 启用远程 API
XML-RPC 和 SOAP 远程 API 从 Confluence 5.5 开始已经废弃了.我们推荐你使用完全支持的Confluence Server REST API. 希望启用 XML-RPC ...
- Mybaits动态Sql
什么是动态SQL? MyBatis的强大之处便是它的动态SQL,如果你使用JDBC那么在根据不同条件查询时,拼接SQL语句是多么的痛苦. 比如查询一个学生信息,可以根据学生的姓名,性别,班级,年龄,学 ...
- Java并发编程基础-ReentrantLock的机制
同步锁: 我们知道,锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能够防止多个线程同时访问共享资源,在Lock接口出现之前,Java应用程序只能依靠synchronized关键字来实现同步锁 ...
- Metasploit one test
1.对Metasploit的文件结构层次做一个目录结构图 2.漏洞利用的原理及其过程 攻击者发送一个附加攻击载荷的漏洞攻击代码给存在漏洞的系统.漏洞攻击代码首先执行,如果执行成功,攻击载荷中的实际代码 ...
- 两种lca的求法:树上倍增,tarjan
第一种:树上倍增 f[x,k]表示x的2^k辈祖先,即x向根结点走2^k步达到的结点. 初始条件:f[x][0]=fa[x] 递推式:f[x][k]=f[ f[x][k-1] ][k-1] 一次bfs ...
- java测试
//信1705-1 20173527 刘津鑫package money;import java.io.IOException;import java.io.Serializable;import ja ...
- python字符串之split
函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(lis ...