题目链接:http://poj.org/problem?id=3614

题意:C头牛去晒太阳,每头牛有自己所限定的spf安全范围[min, max];有L瓶防晒液,每瓶有自己的spf值和容量(能供几头牛用)。

求这L瓶防晒液最多能让多少头牛安全地晒太阳。

思路:贪心策略,按spf从小到大或从大到小的顺序取出防晒液,供给尽可能多的剩余的牛。

具体如何判断当前这瓶防晒液最多能供给几头牛呢?

以spf从小到大排序所有防晒液为例,可以维护一个小顶堆,每取出一瓶防晒液l,就把剩余的所有min值低于l.spf的牛的max值放入堆中。

接下来在l的容量尚未耗尽时,反复弹出并比较堆顶值与l.spf,若大于l.spf,则 l 消耗1单位的容量供给这头牛,计数值加1;否则这头牛不能被任何防晒液供给(当前spf已经是剩余的最小值,后续不会有更小的)。反复取堆顶元素直至容量耗尽或堆变空。各瓶防晒液的计数值的总和即为答案。

首先需要将防晒液按spf值从小大到排序(O(LlogL)),以及将牛按min值从小到大排序(O(ClogC));然后外层循环对L瓶防晒液进行一遍扫描(O(L)),内层循环每头牛的max必然入堆一次、弹出一次(Ω(C)),所以总的复杂度为O(LlogL + CLogC + LC)。

自己实现的堆,时间上总是比STL的priority_queue慢一些,不过空间更少。

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. const int MAX_C = ;
  5. const int MAX_L = ;
  6. int C, L;
  7. struct Cow
  8. {
  9. int min, max;
  10. Cow& operator = (Cow& c){
  11. min = c.min;
  12. max = c.max;
  13. return *this;
  14. }
  15. }cows[MAX_C];
  16.  
  17. struct Lotion
  18. {
  19. int spf,cover;
  20. }lotions[MAX_C];
  21.  
  22. bool cmpL(Lotion l1, Lotion l2){
  23. return l1.spf < l2.spf;
  24. }
  25. bool cmpC(Cow c1, Cow c2){
  26. return c1.min < c2.min;
  27. }
  28.  
  29. int heap[MAX_C]; //小顶堆
  30. int size = ;
  31.  
  32. void swap(int& x, int& y){
  33. int tmp = x;
  34. x = y;
  35. y = tmp;
  36. }
  37.  
  38. void insert(int x){
  39. size++;
  40. heap[size-] = x;//目标元素暂时插到末尾
  41. int i = size - ;//候选目标位置
  42. while(i > ){ //上滤,反复与父节点比较
  43. int p = (i-)/;
  44. if(heap[p] > heap[i]){//与父节点违反堆序性时
  45. swap(heap[i], heap[p]);//父节点下沉
  46. i = p; //候选位置攀升
  47. }else break;
  48. }
  49. }
  50.  
  51. void deleteTop(){
  52. heap[] = heap[size-];
  53. size--;
  54. int i = ; //候选目标位置
  55. while(i*+ < size){//下滤
  56. int lc = i*+;
  57. int rc = i*+;
  58. int c = lc;
  59. if(rc<size && heap[rc]<heap[lc])
  60. c = rc;
  61. if(heap[c] < heap[i]){
  62. swap(heap[c], heap[i]);//孩子节点攀升
  63. i = c;//候选位置下沉
  64. }else break;
  65. }
  66. }
  67.  
  68. int getTop(){
  69. return heap[];
  70. }
  71.  
  72. int main()
  73. {
  74. freopen("3614.txt", "r", stdin);
  75. scanf("%d%d", &C, &L);
  76. for(int i=; i<C; i++){
  77. scanf("%d%d", &cows[i].min, &cows[i].max);
  78. }
  79.  
  80. for(int i=; i<L; i++){
  81. scanf("%d%d", &lotions[i].spf, &lotions[i].cover);
  82. }
  83.  
  84. sort(lotions, lotions+L, cmpL);
  85. sort(cows, cows+C, cmpC);
  86.  
  87. int cnt = ;
  88. for(int i=, j=; i<L; i++){
  89. //printf("lotion %d %d\n", lotions[i].spf, lotions[i].cover);
  90. while(j<C && cows[j].min <= lotions[i].spf){
  91. insert(cows[j].max);
  92. j++;
  93. //printf("insert %d\n", cows[j-1].max);
  94. }
  95. int vol = lotions[i].cover;
  96.  
  97. while(vol > && size>){
  98. if(getTop() >= lotions[i].spf){
  99. vol--;
  100. cnt++;
  101. //printf("add %d\n", getTop());
  102. }
  103. deleteTop();
  104. //printf("%d\n", cnt);
  105. }
  106. }
  107. printf("%d\n", cnt);
  108. return ;
  109. }

【POJ 3614 Sunscreen】贪心 优先级队列的更多相关文章

  1. POJ 3614 Sunscreen 贪心

    题目链接: http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MSMemory Limit: 65536K 问题描述 to avoid ...

  2. POJ 2431 Expedition 贪心 优先级队列

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Descr ...

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

    http://poj.org/problem?id=3614 有c头奶牛在沙滩上晒太阳,每头奶牛能忍受的阳光强度有一个最大值(max_spf) 和最小值(min_spf),奶牛有L种防晒霜,每种可以固 ...

  4. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

  5. HDU 6709“Fishing Master”(贪心+优先级队列)

    传送门 •参考资料 [1]:2019CCPC网络选拔赛 H.Fishing Master(思维+贪心) •题意 池塘里有 n 条鱼,捕捉一条鱼需要花费固定的 k 时间: 你有一个锅,每次只能煮一条鱼, ...

  6. The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)

    传送门 •题意 二维平面上有 n 条线段,每条线段坐标为 $(l_i,i),(r_i,i)$: 平面上的每个整点坐标上都可以放置一枚硬币,但是要求任意两枚硬币的横坐标不相同: 问最多有多少条线段可以放 ...

  7. HDU 6438"Buy and Resell"(贪心+优先级队列)

    传送门 •参考资料 [1]:HDU6438(优先队列+思维) •题意 有n个城市,第 i 天你会达到第 i 个城市: 在第 i 个城市中,你可以用 ai 元购买一个物品,或者用 ai 元卖掉一个物品, ...

  8. Sunscreen POJ - 3614(贪心)

    To avoid unsightly burns while tanning, each of the C (1 ≤ C ≤ 2500) cows must cover her hide with s ...

  9. 优先队列:POJ No 3614 Sunscreen 贪心

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

随机推荐

  1. VS2010下WPF开发ARCGIS ENGINE 10的带Ribbon控件项目

    原文 http://blog.sina.com.cn/s/blog_47522f7f0100nq5t.html 题目好长,但是集目前最新的工具于一身..VS是最新的2010版,不过用的是.net3.5 ...

  2. Fragment销毁时replace和add两个方法的区别

    这个首先从一个bug说起,如图:   我们都知道fragment切换有两种方式: 1. replace方式 transaction.replace(R.id.content, IndexFragmen ...

  3. HTML5视音频小结

    目前,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件.HTML5 规定了一种通过 video 元素来包含视频的标准方法.当前HTML5只支持三种格式的视频. 格 ...

  4. lowerCaseTableNames

    数据库表,数据库名大小写铭感问题 mysql lower-case-table-names参数 线上有业务用到开源的产品,其中SQL语句是大小写混合的,而建表语句都是小写的,mysql默认设置导致这些 ...

  5. UGUI Toggle控件

    今天我们来看看Toogle控件, 它由Toogle + 背景 + 打勾图片 + 标签组成的. 它主要用于单选和多选 属性讲解: Is On: 代表是否选中. Toogle Transition: 在状 ...

  6. Windows Server 2008 R2 开启Win7主题效果Aero

    1.打开 开始---管理工具----服务器管理器--功能 2.点击 “添加功能”,选择“桌面体验”,这样就会安装上win7 主题和Windows media player 3.重启电脑后,在“服务”里 ...

  7. ios8加入通知栏开始

    ios8加入通知栏开始 by 吴雪莹 以打开vpn设置为例: @IBAction func open(sender: AnyObject) { let context = self.extension ...

  8. apache安装扩展模块

    apache 安装扩展模块 1,首先要确认你是否加载了mod_so模块,这个就是你在编译前参数配置的时候添加-enable-so(启用DSO).如果你没有这模块的话,是无法安装扩展模块的. /usr/ ...

  9. winform —— 对话框和流及打印

    对话框:  注意引用using System.IO; showdialog();显示对话框,返回一个dialogresult的枚举类型 colorDialog:color属性,用来获取颜色 folde ...

  10. eclipse美化,全黑eclipse 保护眼睛

    如标题,闲话不多说.有图说明一切.看看这是你想要的嘛? 教程及资源下载地址: http://download.csdn.net/detail/shoneworn/8326097