poj2431优先队列
To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).
The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).
Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.
Input
* Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.
* Line N+2: Two space-separated integers, L and P
Output
Sample Input
4
4 4
5 2
11 5
15 10
25 10
Sample Output
2
Hint
The truck is 25 units away from the town; the truck has 10 units of fuel. Along the road, there are 4 fuel stops at distances 4, 5, 11, and 15 from the town (so these are initially at distances 21, 20, 14, and 10 from the truck). These fuel stops can supply up to 4, 2, 5, and 10 units of fuel, respectively.
OUTPUT DETAILS:
Drive 10 units, stop to acquire 10 more units of fuel, drive 4 more units, stop to acquire 5 more units of fuel, then drive to the town.
#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#define maxn 1000005
using namespace std;
struct Node
{
int dis;
int fuel; }node[maxn];
priority_queue <int> fu;
bool cmp(Node a,Node b)
{ if(a.dis!=b.dis)
return a.dis>b.dis;
}
int main()
{
int n;
cin>>n;
for(int i=;i<n;i++)
{
scanf("%d%d",&node[i].dis,&node[i].fuel);
}
sort(node,node+n,cmp);
int p,l;
cin>>l>>p;
int t=;
bool flag=false;
int res=;
for(int i=l;i>;i--)
{
//cout<<i<<endl;
//cout<<fu.top()<<endl<<endl;
if(p>)
{
p--;
}
else if(p==)
{
for(t;t<n;t++)
{
if(node[t].dis<i) break;
fu.push(node[t].fuel);
}
if(fu.empty())
{
flag=true;
break;
} p=fu.top();
fu.pop();
res++;
p--;
}
}
if(!flag)
{
cout<<res<<endl;
}
else cout<<"-1"<<endl;
return ;
}
poj2431优先队列的更多相关文章
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- POJ2431 Expedition(排序+优先队列)
思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...
- poj2431(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...
- poj2431 Expedition优先队列
Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...
- 【poj2431】驾驶问题-贪心,优先队列
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29360 Accepted: 8135 Descr ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
- POJ 2431 (优先队列)
题目链接:https://vjudge.net/problem/POJ-2431 思路: “ 在卡车行驶途中, 只有经过加油站才能加油.” 我们不妨转变思路, 理解成“当卡车驶过加油站时就获得了加油的 ...
- 【POJ - 2431】Expedition(优先队列)
Expedition 直接中文 Descriptions 一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队.作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱.卡车现在每运行一个单位的距离 ...
- 堆排序与优先队列——算法导论(7)
1. 预备知识 (1) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
随机推荐
- error: commit is a merge but no -m
https://segmentfault.com/q/1010000010185984 执行git cherry-pick commitID操作时报错,如题 原因是合并的commitID做过merge ...
- ylbtech-LanguageSamples-SimpleVariance
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-SimpleVariance 1.A,示例(Sample) 返回顶部 SimpleVar ...
- [转载]Delphi 版 everything、光速搜索代码
近日没啥事情,研究了一下 everything.光速搜索原理.花了一个礼拜时间,终于搞定. 废话不多说,直接上代码: unit uMFTSearchFile; { dbyoung@sina.com 2 ...
- 流畅的python第七章函数装饰器和闭包学习记录
本章讨论的话题 python如何计算装饰器句法 python如何判断变量是不是局部的(通过函数内部是否给变量赋值过来判断是否是局部变量) 闭包存在的原因和工作原理(闭包是一种函数,它会保留定义函数时存 ...
- ISP图像调试工程师——宽动态增强(熟悉图像预处理和后处理技术)
宽动态范围图像快速增强算法 : http://www.docin.com/p-1783311889.html
- 关于mysql字段名和保留字冲突的问题
建了个表,有个字段起名为key,结果insert语句报错了,说是sql不对. 原因:字段key和MySQL的保留字冲突了,当mysql的字段名和保留字冲突的时候,sql语句中的字段名需要加上反引号`` ...
- 在 TDA 工具里看到 Java Thread State 的第一反应是
转载:http://itindex.net/detail/43158-tda-%E5%B7%A5%E5%85%B7-java 使用 TDA 工具,看到大量 Java Thread State 的第 ...
- mac本地搭建wordpress
1 下载安装最新的xampp 2 安装完成后,使用下面的命令开始运行 XAMPP.在终端下以系统管理员 root 的身份登录 管理员root身份登录 sudo su 使用命令启动XAMPP /Appl ...
- zookeeper伪分布式集群安装
1.安装3个zookeeper 1.1创建集群安装的目录 1.2配置一个完整的服务 这里不做详细说明,参考我之前写的 zookeeper单节点安装 进行配置即可,此处直接复制之前单节点到集群目录 创建 ...
- 浅析php中抽象类和接口的概念以及区别[转]
//抽象类的定义: abstract class ku{ //定义一个抽象类 abstract function kx(); ...... } function aa extends ku{ //实现 ...