解题方法详见《挑战程序设计竞赛(第二版)》P74-75。注意首先要对加油站以位置为关键字快排,不要遗忘把终点视作一个加油量为0的加油站,否则最终只能到达终点前的加油站。

 /*优先队列*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=+; struct rec
{
int pos,oil;
bool operator < (const rec& x) const
{
return pos<x.pos;
}
}; rec sta[MAXN];
int n,L,P,a,t=; void input()
{
scanf("%d",&n);
for (int i=;i<n;i++) scanf("%d%d",&sta[i].pos,&sta[i].oil);
scanf("%d%d",&L,&P);
for (int i=;i<n;i++) sta[i].pos=L-sta[i].pos;
sta[n].pos=L;//把终点也作为加油站
sta[n].oil=;
n++;
} void doit()
{
priority_queue<int> pque;
sort(sta,sta+n);
int npos=;
for (int i=;i<n;i++)
{
int d=sta[i].pos-npos;
while (P-d<)
{
if (pque.empty())
{
cout<<-<<endl;
return;
}
t++;
P+=pque.top();
pque.pop();
}
P-=d;
npos=sta[i].pos;
pque.push(sta[i].oil);
}
cout<<t<<endl;
return;
} int main()
{
input();
doit();
return ;
}

【优先队列+贪心】POJ2431-Expedition的更多相关文章

  1. POJ2431 优先队列+贪心 - biaobiao88

    以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...

  2. 最高的奖励 - 优先队列&贪心 / 并查集

    题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...

  3. hdu3438 Buy and Resell(优先队列+贪心)

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  4. H - Expedition 优先队列 贪心

    来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...

  5. POJ2431 Expedition(排序+优先队列)

    思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...

  6. poj2431(优先队列+贪心)

    题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...

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

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

  8. POJ2431 Expedition 贪心

    正解:模拟费用流 解题报告: 先放个传送门鸭,题目大意可以点Descriptions的第二个切换成中文翻译 然后为了方便表述,这里强行改一下题意(问题是一样的只是表述不一样辣,,, 就是说现在在高速公 ...

  9. poj2431 Expedition优先队列

    Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...

  10. ZOJ-3410Layton's Escape(优先队列+贪心)

    Layton's Escape Time Limit: 2 Seconds      Memory Limit: 65536 KB Professor Layton is a renowned arc ...

随机推荐

  1. MSSQL数据库 事务隔离级别

    数据库事务的隔离级别有4个,由低到高依次为Read uncommitted .Read committed .Repeatable read .Serializable ,这四个级别可以逐个解 脏读 ...

  2. 利用最新Apache解析漏洞(CVE-2017-15715)绕过上传黑名单

    转载自:https://www.leavesongs.com/PENETRATION/apache-cve-2017-15715-vulnerability.html 目标环境: 比如,目标存在一个上 ...

  3. 【Python学习】解决pandas中打印DataFrame行列显示不全的问题

    在使用pandas的DataFrame打印时,如果表太长或者太宽会自动只给前后一些行列,但有时候因为一些需要,可能想看到所有的行列. 所以只需要加一下的代码就行了. #显示所有列 pd.set_opt ...

  4. Linking code for an enhanced application binary interface (ABI) with decode time instruction optimization

    A code sequence made up multiple instructions and specifying an offset from a base address is identi ...

  5. web.py输出中文字符串乱码问题的解决

    web.py中文字符串网页显示乱码的解决 #!/usr/bin/env python # encoding: utf-8 import weburls = ( '/', 'index')class i ...

  6. MariaDB 层常用业务

    前言  -  简单准备一下前戏 前面写过几篇mariadb 数据的随笔, 多数偏C/C++层面. 这次分享一下平时开发中, 处理的一些数据层面的业务. 对于MariaDB, 不做过多介绍. 如果你有U ...

  7. python中eval函数使用

    把字符串转换为字典: s = "{'a':1}" eval(s)

  8. android dialog 筛选列表

    1.效果图 2. 布局文件 1)显示筛选的标题 <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  9. 判断ArcSDE是否安装成功

    安装SDE后在ArcMap的Toolboxes - Data Management Tools中会新增Geodatabase Administration 即数据管理工具下面会新增地理数据库管理

  10. IE8下面的line-height的bug

    当line-height小于正常值时,超出的部分将被剪裁掉