汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数。

做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油量大于0。

虽然是道挺水的题目,但还是要注意细节...

  1. /** @Date : 2017-09-21 22:45:37
  2. * @FileName: POJ 2431 优先队列.cpp
  3. * @Platform: Windows
  4. * @Author : Lweleth (SoungEarlf@gmail.com)
  5. * @Link : https://github.com/
  6. * @Version : $Id$
  7. */
  8. #include <stdio.h>
  9. #include <iostream>
  10. #include <string.h>
  11. #include <algorithm>
  12. #include <utility>
  13. #include <vector>
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include <stack>
  18. #include <queue>
  19. #include <math.h>
  20. //#include <bits/stdc++.h>
  21. #define LL long long
  22. #define PII pair<int ,int>
  23. #define MP(x, y) make_pair((x),(y))
  24. #define fi first
  25. #define se second
  26. #define PB(x) push_back((x))
  27. #define MMG(x) memset((x), -1,sizeof(x))
  28. #define MMF(x) memset((x),0,sizeof(x))
  29. #define MMI(x) memset((x), INF, sizeof(x))
  30. using namespace std;
  31.  
  32. const int INF = 0x3f3f3f3f;
  33. const int N = 1e5+20;
  34. const double eps = 1e-8;
  35.  
  36. struct yuu
  37. {
  38. int x, l;
  39. }a[10010];
  40. int n, len, p;
  41. int pos[N*10];
  42.  
  43. int cmp(yuu a, yuu b)
  44. {
  45. if(a.x != b.x)
  46. return a.x > b.x;
  47. return a.l > b.l;
  48. }
  49. int main()
  50. {
  51. int n;
  52. while(~scanf("%d", &n))
  53. {
  54. for(int i = 0; i < n; i++)
  55. {
  56. int x, l;
  57. scanf("%d%d", &x, &l);
  58. a[i].x = x;
  59. a[i].l = l;
  60. }
  61. a[n].x = 0;
  62. a[n].l = 0;
  63. sort(a, a + n + 1, cmp);
  64. scanf("%d%d", &len, &p);
  65. priority_queue<int, vector<int>, less<int> >q;
  66. int ans = 0;
  67. int pos = 0;
  68. for(int i = 0; i <= n; i++)
  69. {
  70. p -= len - a[i].x - pos;
  71. if(p < 0)
  72. {
  73. while(!q.empty() && p < 0)
  74. p += q.top(), q.pop(), ans++;
  75. if(p < 0)//这里要退出 忘写了...
  76. {
  77. ans = -1;
  78. break;
  79. }
  80.  
  81. }
  82. //cout << p << endl;
  83. pos = len - a[i].x;
  84. q.push(a[i].l);
  85. }
  86. if(p < 0)
  87. cout << -1 << endl;
  88. else
  89. cout << ans << endl;
  90. }
  91. return 0;
  92. }

POJ 2431 优先队列的更多相关文章

  1. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  2. poj 2431 【优先队列】

    poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...

  3. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  4. poj - 2431 Expedition (优先队列)

    http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...

  5. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...

  6. POJ 2431——Expedition(贪心,优先队列)

    链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...

  7. 优先队列(挑程)poj 2431

    每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑.还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa. 优先队列直接用stl就可以,简单实 ...

  8. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  9. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

随机推荐

  1. 第六周的PSP

    本周PSP: 本周进度条: 累积进度图:: 本周PSP饼状图:

  2. Java 异常总结

    Throwablede类是 Java 语言中所有错误或异常的超类. 两个子类的实例,Error 和 Exception Error 是 Throwablede 的子类,用于指示合理的应用程序不应该试图 ...

  3. JAVA第三次笔记

  4. 遍历frame中的表单:

    遍历frame中的表单: public void table1() { // 查找frame List<WebElement> iframes = driver.findElements( ...

  5. 【计算机基础】当你在浏览器中输入Google.com并且按下回车之后发生了什么?

    本文转载自:https://github.com/skyline75489/what-happens-when-zh_CN#id9 按下"g"键 接下来的内容介绍了物理键盘和系统中 ...

  6. 【C++】C++的构造函数

    构造函数是特殊的成员函数,只要创建类类型的对象,都要执行构造函数.构造函数的工作是保证每个对象的数据成员具有合适的初始值. class Sales_Item { public: //operation ...

  7. 百度editor编辑器添加新字体

    Ueditor本身自带11种字体,添加如下: 1.找到文件 ueditor/lang/zh-cn/zh-cn.js ,添加字体 'fontfamily': {        'songti': '宋体 ...

  8. 循环 与 next()

  9. HDU2460-Network

    题目 给一个\(n\)个点\(m\)条边的无向连通图,\(Q\)次往图中加边,每次加边后问图中的桥有多少个.(加边后边留着). \(n\le 10^5,m\le 2\times 10^5,Q\le 1 ...

  10. BZOJ 1066:[SCOI2007]蜥蜴(最大流)

    蜥蜴Description在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...