POJ:2431-Expedition
Expedition
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20089 Accepted: 5786
Description
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.
解题心得
- 做这个题是看了挑战那本书才来做的题,结果只是考了一个很简单的思维问题,但是被poj上的题目描述和挑战那本书的题目描述给弄晕了,poj是反着描述的,但是挑战那本书是正向描述的。sad。
- 怎么解题挑战那本书上面说的很清楚了就不多说了。
#include <stdio.h>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1e4+100;
int l,now,res,n;
struct NODE {
int va,pos;
friend bool operator < (const NODE a,const NODE b){
return a.pos < b.pos;
}
}node[maxn];
void init() {
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&node[i].pos,&node[i].va);
scanf("%d%d",&l,&res);
node[n].va = 0;
node[n].pos = l;
for(int i=0;i<n;i++)
node[i].pos = l - node[i].pos;//反向输入的位置给弄正
sort(node,node+n+1);
now = 0;
}
void solve() {
int ans = 0;
priority_queue <int> qu;
for(int i=0;i<=n;i++) {
int dis = node[i].pos - now;
while(dis > res) {
if(qu.empty()){
printf("-1");
return ;
}
res += qu.top();
ans++;
qu.pop();
}
qu.push(node[i].va);
now = node[i].pos;
res -= dis;
}
printf("%d",ans);
}
int main() {
init();
solve();
return 0;
}
POJ:2431-Expedition的更多相关文章
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj:4091:The Closest M Points
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
- POJ 2431 Expedition (STL 优先权队列)
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8053 Accepted: 2359 Descri ...
- poj - 2431 Expedition (优先队列)
http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- 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< ...
- poj 2431 Expedition
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12980 Accepted: 3705 Descr ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- POJ 2431 Expedition (priority_queue或者multiset可解)
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18655 Accepted: 5405 Descr ...
随机推荐
- mysql测试和sysbench工具详解
前言 作为一名后台开发,对数据库进行基准测试,以掌握数据库的性能情况是非常必要的.本文介绍了MySQL基准测试的基本概念,以及使用sysbench对MySQL进行基准测试的详细方法. 文章有疏漏之处, ...
- CSS透明度设置(兼容性)
一句话搞定透明背景! .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0.5; opa ...
- R语言基础命令与安装
1. R的安装过程 1.1.首先附上清华线路的下载链接Windows版3.3.1 1.2. 选择安装路径 1.3. 注意根据自己的计算机位数选择,如我的是64位,便选择64位安装. 1.4. 其他默认 ...
- python网络编程-socketserver模块
使用socketserver 老规矩,先引入import socketserver 必须创建一个类,且继承socketserver.BaseRequestHandler 这个类中必须重写handle( ...
- struts2 中使用DMI(动态调用方法)方式配置action
1.使用 "!"方式,即 action名称 ! 方法名称 struts.xml 配置 <package name="user" namespace=&qu ...
- leetcode:回溯——permutation-sequence,
1. permutation-sequence 顺序排列第k个序列 The set[1,2,3,…,n]contains a total of n! unique permutations. By l ...
- select_related()函数
Django获取数据实体的时候,返回的对象一个实体或多个实体,也就是QuerySet,它是Django专有的东西,具体的理解,它是类似Python的字典的东西,但它并不实现字典的所有方法.今天讲解的是 ...
- 模拟网页的浏览Stack(POJ1028)
题目链接:http://poj.org/problem?id=1028 注意: 1.用两个栈来模拟,一个用来存可以返回的,一个用来存可以前进的. 2.visit方法,就要将可以前进的栈清空. 3.ba ...
- 裁剪插件jCrop
为大家介绍个插件:jCrop.这个插件被我用在了多个项目中,如通过画热力图来查看某块地方用户的浏览数,放大缩小拖动选框来实时预览所选区域的图片病裁剪,设置头像是选框必须要是正方形,它有着丰富的配置参数 ...
- 2018.8.15 AOP面向切面编程简单理解
在Filter过滤器中 拦截器 表面上看 -拦截器帮我们封装了很多功能 拦截器优秀的设计,可拔插设计 aop思想 在struts2中 归纳总结