H - Expedition 优先队列 贪心
来源poj2431
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.
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
Line 1: A single integer, N
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
- Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.
Sample Input
4
4 4
5 2
11 5
15 10
25 10
Sample Output
2
Hint
INPUT DETAILS:
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.
问你要到达目的地,最少需要下几次车,用贪心的思想每次只拿最大的,先对加油站的位置排个序,然后现在油量能到的加油站就push进优先队列,按油量大到小排,一次加一个,到了就跳出,队列空了就到不了
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define scf(x) scanf("%d",&x)
#define scff(x,y) scanf("%d%d",&x,&y)
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+7;
const double eps=1e-8;
const int inf=0x3f3f3f3f;
using namespace std;
const double pi=acos(-1.0);
const int N=1e4+10;
struct node
{
int pos,fue;
}a[N];
priority_queue<int>v;
bool cmp(node a,node b)
{
return a.pos>b.pos;
}
int main()
{
int ans=0,n,l,now;scf(n);
rep(i,0,n)
scff(a[i].pos,a[i].fue);
scff(l,now);
sort(a,a+n,cmp);
int i=0;
while(now<l)
{
for(;i<n;i++)
{
if(l-a[i].pos>now) break;
v.push(a[i].fue);
}
if(v.empty())
{
pf("-1\n");return 0;
}
now+=v.top();
v.pop();
ans++;
}
prf(ans);
return 0;
}
H - Expedition 优先队列 贪心的更多相关文章
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- POJ 2431 Expedition【贪心】
题意: 卡车每走一个单元消耗一升汽油,中途有加油站,可以进行加油,问能否到达终点,求最少加油次数. 分析: 优先队列+贪心 代码: #include<iostream> #include& ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- 1350: To Add Which? (优先队列+贪心 或者 数组模拟)
1350: To Add Which? Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitt ...
随机推荐
- sort函数比较cmp写法
hihocoder1566http://hihocoder.com/problemset/problem/1566 一直WA因为cmp的写法写错了,未能正确实现排序功能. #include<io ...
- EBS WebADI 存储过程增加参数
CREATE OR REPLACE FUNCTION CUX_EXEC_SQL (P_SQL IN VARCHAR2) RETURN NUMBERAS L_CNT NUMBER;BEGIN ...
- APP中的图片如何长按可以下载并保存图片到相册
直接上图 方式一: 实现方式二: 方式三:
- centos安装系统全过程
--查看系统 lsb_release -a --查看端口 netstat -lnp|grep 80 ps 进程ID #查看进程的详细信息 kill -9 进程ID --查看Java 版本 java - ...
- MySQL DBA工作角色和职责介绍
MySQL DBA分架构DBA,运维DBA和开发DBA三种角色,职责介绍如下:
- 微信小程序判断用户是否需要再次授权获取个人信息
一.index.js设置如下 //获取用户的授权信息,放到本地缓存中 wx.getSetting({ success: (res) => { if(res.authSetting['scope. ...
- markdownpad目录格式配置
表格语法 | Tables | Are | Cool | | ------------- |:-------------:| -----:| | col 3 is | right-aligned | ...
- 【PMP】事业环境因素和组织过程资产
事业环境因素(EEFs) 事业环境因素(EEFs):是指组织不能控制的,将对项目产生影响.限制或指令作用的各种条件. ①组织内部的事业环境因素: 组织文化.结构和治理 设施和资源的地理分布 基础设施 ...
- 怎么样加快JavaScript加载和执行效率
概览 无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载 ...
- 阿里云 CentOS7 安装 Nginx 后,无法访问的问题
在阿里云实例中,选择 网络与安全中的安全组.修改安全组规则. 例如: 但是还是不行 需要这样排查 netstat -anp | grep 80 iptables -L -n firewall- ...