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) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
随机推荐
- Flask 在 Debug 模式下初始化2次
请移步: http://blog.zengrong.net/post/2632.html https://stackoverflow.com/questions/9449101/how-to-stop ...
- [转]Sql Server 主从数据库配置
本文转自:http://www.cnblogs.com/yukaizhao/archive/2010/06/02/sql-server-master-slave-mode.html 网站规模到了一定程 ...
- 通用Json的处理办法
1.Json的格式: 对象{"name": "value", "name1": "value1"} 对象包含对象数组{& ...
- ShopNC B2B2C多用户商城2014商业版,带微商城
据说价值7000RMB,咱们好站长资源网友分享出来的,非常感谢分享这么好的源码.此套ShopNC B2B2C多用户商城源码是2014版的,带有微商城,源码我们安装测试基本没发现啥问题,这两天将会完全免 ...
- IE测试CSS兼容性测试
我们知道IE6~8是现在浏览器的主流.但是由IE6开始,我们已经知道IE并不是完全执行W3C标准.我们在编程的时候往往遇到只兼容某一种浏览器. 我们以前经常使用IE Test进行IE的兼容性测试.但是 ...
- java线程总结(1/5)
前言 闲来无事正值面试,看面试中有线程之问题,特此总结一番. 正文 一.线程和进程的区别:1.每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销.2.线程可以看成时轻量级的进程 ...
- 机器学习基石第一讲:the learning problem
博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) Andrew Ng的Machine Learning比較简单,已经看完.林田轩的机器 ...
- vue - webpack.dev.conf.js for HtmlWebpackPlugin
描述: 这是一个webpack插件,可以简化HTML文件的创建,为您的webpack捆绑服务提供服务. 这对于webpack包含文件名中包含哈希值的bundle 来说尤其有用,它会更改每个编译. 您可 ...
- JSP语法学习笔记
1.修改JSP页面模版:找到MyEclips安装目录,搜索“Jsp.vtl”,找到该文件修改编码,以及一些不需要用到的代码. 2.查找项目生成的Servlet文件路径:查看当前项目父级目录搜索 .me ...
- ant-design getFieldDecorator 无法获取自定义组件的值
1.自定义或第三方的表单控件,也可以与 Form 组件一起使用.只要该组件遵循以下的约定: (1)提供受控属性 value 或其它与 valuePropName 的值同名的属性. (2)提供 onCh ...