poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431
你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇,在当前位置和城镇之间有n个加油站可以加油。
为了减少危险,需要最少的加油次数,卡车的油箱可以看作无限大,卡车初始距离城镇L单位,自身有P单位的油。
注意输入的距离是与城镇的距离不是与开始点的距离。转换一下就好。
思想:把经过的所有加油站都加入优先队列,当燃料不足时就取出优先队列的最大元素,用来给卡车加油,如果优先队列也是空的,就不能到达终点。
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = + ;
int L, P, N;
struct node
{
int x,y; //x是与初始点的距离 y是这个加油站有多少油
bool operator < (const node &a) const
{
return x < a.x; //按与初始点的距离排序
}
}f[maxn]; void solve() {
f[N].x = L; //把终点也加入,但是油 为0 为了方便处理
f[N].y = ;
N++; priority_queue<int> que; int ans = , pos = , tank = P; // ans: 加油次数 , pos:现在所在位置 ,tank:邮箱中汽油的量
for(int i = ; i < N; i++) {
int d = f[i].x - pos; //接下来要前进的距离
while(tank - d < ) { //如果油不够 就不断加油
if(que.empty()) {
puts("-1");
return;
}
tank += que.top();
que.pop();
ans++;
}
tank -= d;
pos = f[i].x;
que.push(f[i].y);
}
printf("%d\n", ans);
} int main()
{
scanf("%d",&N);
for(int i = ; i < N; i++) {
scanf("%d%d",&f[i].x,&f[i].y);
}
scanf("%d%d",&L,&P);
for(int i = ; i < N; i++) {
f[i].x = L - f[i].x;
}
sort(f,f+N);
solve();
return ;
}
poj - 2431 Expedition (优先队列)的更多相关文章
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- 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 (贪心 + 优先队列)
题目链接: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 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
随机推荐
- Eclipse支持Jquery代码提示(JqeuryWTP)
问题描述: Eclipse支持Jquery代码提示 问题解决: 下载 JqueryWTP.jar文件 文件替换 在Eclipse/plugin 路径下, ...
- 【BZOJ】【1485】【HNOI2009】有趣的数列
Catalan数/组合数取模 Aha!这题我突然灵光一现就想到Catalan数……就是按顺序安排1~2n这些数(以满足前两个条件)……分配到奇数位置上的必须比偶数位置上的多(要不就不满足第三个条件了) ...
- 日志:slf4j+log4j+maven配置
代码的日志输出,前前后后折腾了我好几次. 本着会用,快速配置的原则,还是将配置过程记录下来,以便复用. 参考:http://blog.csdn.net/anialy/article/details/8 ...
- CoreText 使用教程
[iOS开发] CoreText 使用教程:以创建一个简单的杂志应用为例抢沙发 分类:iPhone开发 标签:CoreText.iOS.iOS开发.iOS开发教程.杂志应用 BBS.CHINAAPP. ...
- 如何将控制台程序包装成windows服务
1. 新建一个项目,或者从选择当前解决方案--右键-添加--新建项目 2. 选择(项目类型)Visual C#项目,(模板)Windows 服务,填写要创建的服务名称(修改默认的WindowServi ...
- Native App执行JS
iOS: - (void)webViewDidFinishLoad:(UIWebView *)webView{ //js方法名+参数 NSString* jsCode = [NSS ...
- ISIN编码
国际证券识别编码(ISIN编码)是由国际标准化组织(ISO)制定的证券编码标准,并在<证券及相关金融工具-国际证券识别编码体系>(ISO6166)中正式发布.ISO6166主要规定了ISI ...
- MariaDB Galera Cluster集群
一.MariaDB Galera Cluster概要: 1.简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步 ...
- Jsp helloWorld
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEnco ...
- SQL技术内幕-13 SQL优化方法论之分离重量级的等待
Code -- Isolate top waits WITH Waits AS ( SELECT wait_type, wait_time_ms / . AS wait_time_s, . * wai ...