poj2431 Expedition优先队列
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.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> using namespace std;
int n;
int l, p;
struct Fuel{
int disD;
int disS;
int c;
};
Fuel fuel[]; int cmp(const void *a, const void *b) {
Fuel at = *(Fuel *)a;
Fuel bt = *(Fuel *)b;
return at.disS - bt.disS;
} priority_queue <int> fque; int main(int argc, char const *argv[])
{
while(scanf("%d",&n) != EOF) {
for(int i = ; i < n; i++) {
int tmp;
scanf("%d %d",&fuel[i].disD,&fuel[i].c);
}
scanf("%d %d",&l, &p);
for(int i = ; i < n; i++) {
fuel[i].disS = l - fuel[i].disD;
}
fuel[n].disS = l;
fuel[n].c = ;
qsort(fuel,n, sizeof(Fuel), cmp); while(!fque.empty()) {
fque.pop();
}
int ans = ;
int now = ;
int flag = true;
for(int i = ; i <= n && flag; i++) {
int remain = p - fuel[i].disS + now;
while(remain < ) {
if(fque.empty()) {
flag = false;
break;
}
int tmp = fque.top();
fque.pop();
p = p + tmp;
remain = remain + tmp;
ans++;
}
if(remain >= ) {
fque.push(fuel[i].c);
now = fuel[i].disS;
p = remain;
} }
if(now == l) {
printf("%d\n",ans);
}
else {
puts("-1");
} }
return ;
}
poj2431 Expedition优先队列的更多相关文章
- poj 3431 Expedition 优先队列
		poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ... 
- POJ2431 Expedition(排序+优先队列)
		思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ... 
- H - Expedition  优先队列 贪心
		来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ... 
- poj2431(优先队列+贪心)
		题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ... 
- EXPEDI - Expedition 优先队列
		题目描述 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rathe ... 
- poj2431  Expedition
		直接代码... #include<string.h> #include<stdio.h> #include<queue> #include<iostream& ... 
- POJ2431 Expedition 贪心
		正解:模拟费用流 解题报告: 先放个传送门鸭,题目大意可以点Descriptions的第二个切换成中文翻译 然后为了方便表述,这里强行改一下题意(问题是一样的只是表述不一样辣,,, 就是说现在在高速公 ... 
- 【POJ - 2431】Expedition(优先队列)
		Expedition 直接中文 Descriptions 一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队.作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱.卡车现在每运行一个单位的距离 ... 
- POJ2431 优先队列+贪心 - biaobiao88
		以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ... 
随机推荐
- hihocoder1822 战舰日常任务
			思路: 使用堆即可. 实现: #include <iostream> #include <map> #include <vector> #include <c ... 
- WPF之Binding【转】
			WPF之Binding[转] 看到WPF如此之炫,也想用用,可是一点也不会呀. 从需求谈起吧: 首先可能要做一个很炫的界面.见MaterialDesignInXAMLToolKit. 那,最主要的呢, ... 
- 安卓ListView基础应用
			listview简单描述 主页面: package com.example.listview; import com.lidroid.xutils.ViewUtils; import com.lidr ... 
- wxwidgets编译及环境配置
			wxwidgets编译及环境配置 安装步骤: 到www.CodeBlocks.org下载并安装CodeBlocks,最好下载MinGW版本的,可以省掉安装和配置GCC的麻烦. 到www.wxWidge ... 
- java中的堆与栈
			Java 中的堆和栈 Java把内存划分成两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型的变量和对象的引用变量都在函数的栈内存中分配 . 当在一段代码块定义一个变量时,Java就在栈中 ... 
- IOS7.1 企业应用 证书无效 已解决
			http://www.cocoachina.com/bbs/read.php?tid=194213&keyword=7.1 关于IOS7.1企业版发布后,用户通过SARAFI浏览器安装无效的解 ... 
- HTML5微信播放全屏问题的解决方法
			在ios和安卓手机里的微信下播放视频时,会遇到不少问题,例如需要手动点击,视频才会播放,并且视频会跳出微信框,出现控制条,如果视频不是腾讯视频,播放完毕会出现腾讯视频的广告推送等问题 解决办法:给vi ... 
- 玩4K必备知识:HDMI1.4、2.0、2.0a、2.0b接口参数对比【扫盲贴】
			https://www.4k123.com/thread-55369-1-1.html 前言:玩4K的同学都知道,HDMI接口是视频传输最常用的接口,但是这个接口却有好几个版本HDMI1.4.HDMI ... 
- vue引用文件
			1)css引入在vue页面中<style scoped> @import url("../assets/css/home.css");</style>2)j ... 
- Linux下MongoDB的安装、配置、启动
			下载 MongoDB下载地址为 https://www.mongodb.org/downloads 安装 将文件放到/usr/local/目录下并解压 tar -zxvf mongodb-linux- ... 
