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) 基本概念 如图,(二叉)堆是一个数组,它可以被看成一个近似的完全二叉树.树中的每一个结点对应数组中的一个元素.除了最底层外,该树是完全充满的,而且从左向右填充.堆的数组 ...
随机推荐
- Delphi控件开发浅入深出(三)
三.开关控件TlincoSwitch 用过Delphi1(好古老的东东呀!)的人相信都记得这个开关控件 ,不知道当初Borland为什么把这么一个在开发普通应用程序中应用不到的工控控件放到Delphi ...
- 一个手绘normal的方法
https://polycount.com/discussion/98983/how-to-paint-flow-anisotropic-comb-maps-in-photoshop flow map ...
- linux内核设计学习
负载平衡程序 load_balance调用条件:只要当前可执行队列为空,它就会被调用.被定时器调用:系统空闲时每隔1毫秒调用一次或其他情况下每隔200mm调用一次.单处理器不会被调用 执行步骤1. 首 ...
- DevExpress 中 DateEdit 控件 格式化显示和编辑的日期格式为: yyyy-MM-dd
摘自: http://blog.sina.com.cn/s/blog_76b5256c0100zkwk.html 1需要显示的日期为2012年3月12日需要如下设置 Properties-Mask-E ...
- Java笔记13:统计文件中每个字符出现的次数
一.代码实现 import java.io.*; import java.util.*; /** 功能:统计文件中每个字符出现的次数 思路: 1.定义字符读取(缓冲)流 2.循环读取文件里的字符,用一 ...
- Spark Streaming资源动态分配和动态控制消费速率
本篇从二个方面讲解: 高级特性: 1.Spark Streaming资源动态分配 2.Spark Streaming动态控制消费速率 原理剖析,动态控制消费速率其后面存在一套理论,资源动态分配也有一套 ...
- RocketMQ里的一个获取时间的工具类SystemClock
看rocketmq源码的时候发现他们还给时钟封装里一下. /** * Licensed to the Apache Software Foundation (ASF) under one or mor ...
- 在android中画圆形图片的几种办法
在开发中常常会有一些需求,比方显示头像,显示一些特殊的需求,将图片显示成圆角或者圆形或者其它的一些形状. 可是往往我们手上的图片或者从server获取到的图片都是方形的.这时候就须要我们自己进行处理, ...
- 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结
不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...
- 实现淡入淡出效果的组件,继承自JComponent
由于仅贴出代码,供有缘人参考. import java.awt.AlphaComposite; import java.awt.Graphics; import java.awt.Graphics2D ...