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 ...
随机推荐
- ABC时间管理法
名称 ABC时间管理法 属于 事务优先顺序法的“鼻祖” 做法 将待办的事项按照又重要到轻的顺序划分为A,B,C三个等级,然后按照事项的重要等级依据完成任务的做事方法. 特点 使学习.工作和生活等活动在 ...
- 重新安装Magento2 Module
如何重新安装Magento2的Module? 分类 2 类: Module是通过 composer 安装的:在命令行输入:php bin/magento module:uninstall Module ...
- babel-loader7和babel8版本的问题
根据官网https://www.npmjs.com/package/babel-loader要对应版本 一.babel7.X版本 1.要安装的包 第1套包:npm i babel-core babe ...
- 转:android 屏幕适配小结
做android开发,开源嘛,满市场都是凌乱的机型,总少不了适配这样或那样的型号.在这里分享一下自己在开发中用到的方法. 首先要介绍一下drawable-mdpi.drawable-hdpi-1280 ...
- UVA 1664 Conquer a New Region (Kruskal,贪心)
题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...
- Gradle环境下导出Swagger为PDF
更多精彩博文,欢迎访问我的个人博客 说明 我个人是一直使用Swagger作为接口文档的说明的.但是由于在一些情况下,接口文档说明需要以文件的形式交付出去,如果再重新写一份文档难免有些麻烦.于是在网上看 ...
- Linux文件系统概述二
VFS-目录项对象(dentry) 每个文件除了有一个索引节点 inode 数据结构外,还有一个目录项 dentry 数据结构 dentry 结构代表的是逻辑意义上的文件,描述的是文件逻辑上的属性,目 ...
- bootstrap下拉菜单(Dropdowns)
本章将重点讲解bootstrap下拉菜单(Dropdowns),下拉菜单是可切换的,是以列表格式显示链接的上下文菜单. <!DOCTYPE html><html><hea ...
- abaqus二次开发概述
说明 abaqus二次开发概述 导语 用户子程序特点 abaqus用户程序接口与调用方式 abaqus用户子程序分类 常用用户子程序介绍 Refence 说明 本系列文章本人基本没有原创贡献,都是在学 ...
- [LUOGU] P1387 最大正方形
题目描述 在一个n*m的只包含0和1的矩阵里找出一个不包含0的最大正方形,输出边长. 输入输出格式 输入格式: 输入文件第一行为两个整数n,m(1<=n,m<=100),接下来n行,每行m ...