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 ...
随机推荐
- 【Java-JPA】让Springboot启动不检查JPA的数据源配置
#https://stackoverflow.com/questions/24074749/spring-boot-cannot-determine-embedded-database-driver- ...
- Python 汉字转拼音
本文参考: Python中文转拼音代码(支持全拼和首字母缩写) 中文中不可以有“()” # -*- coding: utf-8 -*- __version__ = '0.9' __all__ = [& ...
- 柳青(Jean)英文演讲集合
1.Didi Chuxing's Jean Liu on The Future of Cities https://www.youtube.com/watch?v=G9uPGoN0dvQ 2.Did ...
- 【T09】要认识到TCP是一个可靠的,但不是绝对可靠的协议
1.稍微想一下就知道,TCP不是绝对可靠的协议,比如:网络断开,主机崩溃,无论TCP如何努力,都无法将数据传给对方. 2.考虑应用程序A向应用程序B发送数据的TCP流程,数据流从应用程序A通过他所在主 ...
- grid - 使用相同的名称命名网格线和设置网格项目位置
1.使用repeat()函数可以给网格线分配相同的名称.这可以节省一定的时间 使用repeat()函数可以给网格线命名,这也导致多个网格线具有相同的网格线名称. 相同网格线名称指定网格线的位置和名称, ...
- PL/SQL学习笔记之包
一:包 包是由一组相关的函数,过程,变量,游标等PL/SQL程序设计元素的组合而成的一个PL/SQL程序单元,相当于Java中的类. 包的主要作用是封装:把相同或相似的东西归类,方便维护和管理,提高开 ...
- 【一天一个shell命令】【cut】
1. 命令简介 cut根据指定的定界符,切分文件,并将选中的列输出到标准输出. 2. 用法 cut [选项]... [文件]... 打印输入行的选中的parts 到标准输出 3. 选项 4. 示例 以 ...
- 【Linux】常见公共DNS地址
如果您是程序员.系统管理员或任何类型的 IT 工作者,那么您可能有自己最喜欢的用于故障排除的 IP 地址.而且你可能已经用了好几年了. 这些 ip 可用于: ping 测试连接 使用 dig 或 ns ...
- CoreGraphics之CGContextSaveGState与UIGraphicsPushContext
转至:https://www.jianshu.com/p/be38212c0f79 CoreGraphics与UIKit 这边从iOS绘图教程 提取一些重要的内容. Core Graphics Fra ...
- mysqldump详解之--master-data
在前一篇文章中,有提到mysqldump的--single-transaction参数.另外还有个很重要,也是运维中经常用到的参数:--master-data,网上很多关于MySQL不停机备份的实现都 ...