poj 2431 Expedition
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12980 | Accepted: 3705 |
Description
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.
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int N_MAX = ;
struct fuel_stop {
int ditance;
int amount;
bool operator <(const fuel_stop&b)const {
return amount<b.amount || (amount == b.amount &&this->ditance>b.ditance);
}
};
const bool cmp(const fuel_stop&a,const fuel_stop&b){
return a.ditance < b.ditance;
}
priority_queue<fuel_stop>que;
fuel_stop fuel[N_MAX+];
int main() {
int N;
while (cin >> N) {
int dist[N_MAX];
for (int i = ;i <N;i++)
scanf("%d%d",&dist[i],&fuel[i].amount);
int L, P;
cin >> L >> P;
for (int i = ;i <N;i++)
fuel[i].ditance = (L - dist[i]);
sort(fuel,fuel+N,cmp);
fuel[N].amount = ;fuel[N].ditance = L;//把终点当做一个特殊的加油站点
int pos = ,tank=P,ans=;//ans为加油次数,tank为油箱中油量,pos为当前位置
for (int i = ;i <=N;i++) {
int d = fuel[i].ditance - pos;//d为当前距离下一个加油站的距离
while (tank < d) {
if (que.empty()) {
cout<<-<<endl;
return ;
}
tank += que.top().amount;
ans++;
que.pop();
}
tank -= d;
pos = fuel[i].ditance;
que.push(fuel[i]);
}
cout << ans << endl;
}
return ;
}
poj 2431 Expedition的更多相关文章
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- 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 (贪心 + 优先队列)
题目链接: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 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
随机推荐
- 【剑指offer】近期公共祖先
转载请注明出处:http://blog.csdn.net/ns_code/article/details/28113959 剑指offer上的最后一题了,一个递归函数调了一下午,才得到正确的结果. 题 ...
- Java中介者设计模式
中介者设计模式是一种很常见的设计模式,当中我们最为熟悉的就是我们的MVC框架.当中的C作为控制器就是一个详细的中介者,它的作用是把业务逻辑(Model),和视图(Viwe)隔离开来.使M V协调工作, ...
- 优秀js插件收藏
1. 滚动视差效果,类似锤子主页等效果实现 https://github.com/hahnzhu/parallax.js 2. jQuery全屏滚动插件 http://www.dowebok.com/ ...
- android144 360 快捷方式
package com.example; import android.net.Uri; import android.os.Bundle; import android.app.Activity; ...
- Link List
At first, i prepared to go through <the introduction to algorithm> ,however , i found some par ...
- [转]UINavigationController 返回的方法汇总
[html] view plaincopy [self.navigationController popViewControllerAnimated:YES]; UINavigationControl ...
- 利用 Composer 一步一步构建自己的 PHP 框架(四)——使用 ORM
本教程示例代码见 https://github.com/johnlui/My-First-Framework-based-on-Composer 回顾 经过前三篇文章 基础准备 . 构建路由 和 设计 ...
- C#文件操作系列(一)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- pgpool postgresql集群、中间件
pgpool-II是一个工作于PostgreSQL服务器端和PostgreSQL客户端之间的中间件,它提供了如下的功能: 1.连接池 pgpool-II中保存了到PostgreSQL服务器的连接,然后 ...
- 关于js判断鼠标移入元素的方向——上下左右
一开始我是这么想的,将待移入的元素分割四块,用mousemove获取第一次鼠标落入的区域来判断鼠标是从哪个方向进去的. 所以只要写个算法来判断鼠标的值落入该元素的区域就可以得出鼠标移入的方向,如下图: ...