POJ 2431 优先队列
汽车每过一单位消耗一单位油,其中有给定加油站可加油,问到达终点加油的最小次数。
做法很多的题,其中优先对列解这题是很经典的想法,枚举每个加油站,判断下当前油量是否小于0,小于0就在前面挑最大几个直至油量大于0。
虽然是道挺水的题目,但还是要注意细节...
- /** @Date : 2017-09-21 22:45:37
- * @FileName: POJ 2431 优先队列.cpp
- * @Platform: Windows
- * @Author : Lweleth (SoungEarlf@gmail.com)
- * @Link : https://github.com/
- * @Version : $Id$
- */
- #include <stdio.h>
- #include <iostream>
- #include <string.h>
- #include <algorithm>
- #include <utility>
- #include <vector>
- #include <map>
- #include <set>
- #include <string>
- #include <stack>
- #include <queue>
- #include <math.h>
- //#include <bits/stdc++.h>
- #define LL long long
- #define PII pair<int ,int>
- #define MP(x, y) make_pair((x),(y))
- #define fi first
- #define se second
- #define PB(x) push_back((x))
- #define MMG(x) memset((x), -1,sizeof(x))
- #define MMF(x) memset((x),0,sizeof(x))
- #define MMI(x) memset((x), INF, sizeof(x))
- using namespace std;
- const int INF = 0x3f3f3f3f;
- const int N = 1e5+20;
- const double eps = 1e-8;
- struct yuu
- {
- int x, l;
- }a[10010];
- int n, len, p;
- int pos[N*10];
- int cmp(yuu a, yuu b)
- {
- if(a.x != b.x)
- return a.x > b.x;
- return a.l > b.l;
- }
- int main()
- {
- int n;
- while(~scanf("%d", &n))
- {
- for(int i = 0; i < n; i++)
- {
- int x, l;
- scanf("%d%d", &x, &l);
- a[i].x = x;
- a[i].l = l;
- }
- a[n].x = 0;
- a[n].l = 0;
- sort(a, a + n + 1, cmp);
- scanf("%d%d", &len, &p);
- priority_queue<int, vector<int>, less<int> >q;
- int ans = 0;
- int pos = 0;
- for(int i = 0; i <= n; i++)
- {
- p -= len - a[i].x - pos;
- if(p < 0)
- {
- while(!q.empty() && p < 0)
- p += q.top(), q.pop(), ans++;
- if(p < 0)//这里要退出 忘写了...
- {
- ans = -1;
- break;
- }
- }
- //cout << p << endl;
- pos = len - a[i].x;
- q.push(a[i].l);
- }
- if(p < 0)
- cout << -1 << endl;
- else
- cout << ans << endl;
- }
- return 0;
- }
POJ 2431 优先队列的更多相关文章
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- 优先队列(挑程)poj 2431
每次写poj的题都很崩溃,貌似从来没有一次一发就ac的,每次都有特别多的细节需要考虑.还有就是自己写的太粗糙了,应该把每种情况都想到的,总是急着交,然后刷一页wa. 优先队列直接用stl就可以,简单实 ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
随机推荐
- 第六周的PSP
本周PSP: 本周进度条: 累积进度图:: 本周PSP饼状图:
- Java 异常总结
Throwablede类是 Java 语言中所有错误或异常的超类. 两个子类的实例,Error 和 Exception Error 是 Throwablede 的子类,用于指示合理的应用程序不应该试图 ...
- JAVA第三次笔记
- 遍历frame中的表单:
遍历frame中的表单: public void table1() { // 查找frame List<WebElement> iframes = driver.findElements( ...
- 【计算机基础】当你在浏览器中输入Google.com并且按下回车之后发生了什么?
本文转载自:https://github.com/skyline75489/what-happens-when-zh_CN#id9 按下"g"键 接下来的内容介绍了物理键盘和系统中 ...
- 【C++】C++的构造函数
构造函数是特殊的成员函数,只要创建类类型的对象,都要执行构造函数.构造函数的工作是保证每个对象的数据成员具有合适的初始值. class Sales_Item { public: //operation ...
- 百度editor编辑器添加新字体
Ueditor本身自带11种字体,添加如下: 1.找到文件 ueditor/lang/zh-cn/zh-cn.js ,添加字体 'fontfamily': { 'songti': '宋体 ...
- 循环 与 next()
- HDU2460-Network
题目 给一个\(n\)个点\(m\)条边的无向连通图,\(Q\)次往图中加边,每次加边后问图中的桥有多少个.(加边后边留着). \(n\le 10^5,m\le 2\times 10^5,Q\le 1 ...
- BZOJ 1066:[SCOI2007]蜥蜴(最大流)
蜥蜴Description在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到 ...