解题方法详见《挑战程序设计竞赛(第二版)》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. C# 关于调用微信接口的代码

    调用微信接口前需要准备的内容. 1.微信公众平台的appid 2.微信公众平台的secret 3..获取tokenid 4.获取ticket 5.生成签名的随机串 6.生成签名的时间戳 7.生成签名 ...

  2. 使用Sysmon分析宏病毒(Macros Downloader)

    样本为一个Word文件,Virustotal地址: https://www.virustotal.com/#/file/f8aede78ad92bd28f5f699b677d7d5fd362c8be8 ...

  3. java解析XML之DOM解析和SAX解析(包含CDATA的问题)

    Dom解析功能强大,可增删改查,操作时会将XML文档读到内存,因此适用于小文档: SAX解析是从头到尾逐行逐个元素解析,修改较为不便,但适用于只读的大文档:SAX采用事件驱动的方式解析XML.如同在电 ...

  4. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  5. POJ1014(多重背包)

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65044   Accepted: 16884 Descri ...

  6. 设计模式之笔记--桥接模式(Bridge)

    桥接模式(Bridge) 定义 桥接模式(Bridge),将抽象部分与它的实现部分分离,使它们都可以独立地变化. 类图 描述 Abstraction:定义抽象部分的接口,通常在这个接口里面要维护一个实 ...

  7. js-xlsx操作excel表格

    1导入与导出功能实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  8. API(选项/数据 选项/dom)

    选项/数据 data 类型: Object | Function 限制: 组件的定义只接受function var data = { a: 1 } // 直接创建一个实例 var vm = new V ...

  9. HTML+CSS+JS(+Vue)写一个通讯录组件

    求各位大大的Star(*/ω\*). 没有录屏,所以上传的是图片.后面已补充录屏效果. 效果:(主要是参考小米Note3的通讯录的效果做的) 主要功能: 1. 滚动后,通讯录的模块标题会固定在顶部(图 ...

  10. nginx升级步骤

    今天应开发的需求,需要在Nginx增加一个模块,并不能影响现有的业务,所以就必须要平滑升级Nginx,好了,不多说了 1:查看现有的nginx编译参数 /usr/local/nginx/sbin/ng ...