Expedition---poj2431(优先队列-堆的实现)
题目链接:http://poj.org/problem?id=2431
题意:一辆卡车需要行驶 L 距离,车上油的含量为 P,在行驶的过程中有 n 个加油站 每个加油站到终点的距离是ai,每个加油站最多给卡车加 b 单位的油;求最后到达终点需要加油的最少次数;如果不能到达,输出-1;
我们可以从起点开始算,看每次油箱中的油是否能够到达下一个加油站,如果不能就加上已经经过的加油站的所能加的最大加油量,如果能到达,就记录一下此加油站可以被加进去;因为我们每次都需要加进去最大的加油
量,所以我们可以用优先队列;来存,那么每次取出来的就是最大值;
时间复杂度是O(n*logn)
用c++中的STL:
#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 101000
#define INF 0xfffffff
using namespace std; int n, P, L, ans, f; struct node
{
int a,b;
} s[N]; int cmp(node n1,node n2)
{
return n1.a<n2.a;
} void slove()
{
priority_queue<int> Q;///优先队列; int pos = , i = ;///pos表示当前所在位置; while(i <= n)
{
int d = s[i].a - pos; while(Q.size() && P < d)///当不能到达下一个加油站时,要取出最大的加油量加进去;
{
int p = Q.top(); Q.pop();
P += p;
ans ++;
}
if(P >= d)
{
Q.push(s[i].b);
pos = s[i].a;
P -= d;
}
else
{
ans = -;
return ;
}
i ++;
}
} int main()
{
int i; while(scanf("%d",&n) != EOF)
{
memset(s, , sizeof(s)); ans=; for(i=; i<n; i++)
scanf("%d %d",&s[i].a, &s[i].b); scanf("%d %d", &L, &P); for(i=; i<n; i++)
s[i].a = L - s[i].a; s[i].a = L;
s[i].b = ;///把终点当成最后一个加油站处理; sort(s, s+n+, cmp); slove(); printf("%d\n", ans);
}
return ;
}
手写实现堆---优先队列;
#include<stdio.h>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 11000
#define INF 0x3f3f3f3f
using namespace std; int Max_Heap[N], K;///K代表Max_Heap的个数;
int p; void Push(int x)///在最大堆中插入x;
{
Max_Heap[K++] = x;///放到最后一个节点处; int t = K-; while(Max_Heap[t] > Max_Heap[t/] )///当它的值大于它的父节点的值时,要交换一下位置;
{
swap(Max_Heap[t], Max_Heap[t/]);
t = t/;
}
}
void Pop()
{
K --;///删除元素,堆中总个数减一; Max_Heap[] = Max_Heap[K];///把最后一个数放到最上面,然后一步一步往下更新堆; Max_Heap[K] = -;///防止越界,相当于建立哨兵; int t = ;///从第一个节点开始往下更新; while( Max_Heap[t] < Max_Heap[t*] || Max_Heap[t] < Max_Heap[t*+])
{
if(Max_Heap[t] < Max_Heap[t*] && Max_Heap[t*] > Max_Heap[t*+])
{///左儿子大于它时,并且满足左儿子大于右儿子,让左儿子上去;
swap(Max_Heap[t], Max_Heap[t*]);
t = t*;
}
else
{
swap(Max_Heap[t], Max_Heap[t*+]);
t = t* + ;
}
}
} int n, P, L, ans, f; struct node
{
int a,b;
} s[N]; int cmp(node n1,node n2)
{
return n1.a<n2.a;
} void slove()
{
memset(Max_Heap, -, sizeof(Max_Heap)); Max_Heap[] = INF; K = ; int pos = , i = ; while(i <= n)
{
int d = s[i].a - pos; while(K!= && P < d)
{
int p = Max_Heap[];
Pop();
P += p;
ans ++;
}
if(P >= d)
{
Push(s[i].b);
pos = s[i].a;
P -= d;
}
else
{
ans = -;
return ;
}
i ++;
}
} int main()
{
int i; while(scanf("%d",&n) != EOF)
{
memset(s, , sizeof(s)); ans=; for(i=; i<n; i++)
scanf("%d %d",&s[i].a, &s[i].b); scanf("%d %d", &L, &P); for(i=; i<n; i++)
s[i].a = L - s[i].a; s[i].a = L;
s[i].b = ; sort(s, s+n+, cmp); slove(); printf("%d\n", ans);
}
return ;
}
Expedition---poj2431(优先队列-堆的实现)的更多相关文章
- POJ2431 Expedition(排序+优先队列)
思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...
- 【POJ - 2431】Expedition(优先队列)
Expedition 直接中文 Descriptions 一群奶牛抓起一辆卡车,冒险进入丛林深处的探险队.作为相当差的司机,不幸的是,奶牛设法跑过一块岩石并刺破卡车的油箱.卡车现在每运行一个单位的距离 ...
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- Java学习笔记--PriorityQueue(优先队列)(堆)
PriorityQueue(优先队列)实际上是一个堆(不指定Comparator时默认为最小堆)队列既可以根据元素的自然顺序来排序,也可以根据 Comparator来设置排序规则.队列的头是按指定排序 ...
- c++优先队列(堆)
1.最小堆.最大堆 priority_queue<int,vector<int>,greater<int> > f; //最小堆(后面的数逐渐greater) pr ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- 剑指 Offer 40. 最小的k个数 + 优先队列 + 堆 + 快速排序
剑指 Offer 40. 最小的k个数 Offer_40 题目描述 解法一:排序后取前k个数 /** * 题目描述:输入整数数组 arr ,找出其中最小的 k 个数.例如,输入4.5.1.6.2.7. ...
- POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门 Expedition Time Limit: 1000MS Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...
随机推荐
- QTableView修改数据后弹出是否保存的提示框。
自定义CustomDelegate继承自QStyledItemDelegate,重写setModelData(self, editor, model, index)方法 def setModelDat ...
- par函数的adj 参数- 控制文字的对齐方式
adj 用来控制文字的对齐方式,取值范围为0到1,控制图片中x轴和y轴标签,标题,以及通过text 添加的文字的对齐方式 0表示左对齐,代码示例: par(adj = 0)plot(1:5, 1:5, ...
- c++ 的vector、array和数组的比较
ref: http://blog.csdn.net/haust_wang/article/details/49848169
- JavaStuNote 5
接口 (interface) 一个抽象类,全部的方法都是抽象的,全部方法的public, 我们把这种类叫做极度抽象类,是最干瘪的类. public abstract class A { public ...
- Ajax在ASP.NET MVC中上传
HomeController.cs using System; using System.Collections.Generic; using System.Linq; using System.We ...
- 详解JQuery Ajax 在asp.net中使用总结
自从有了JQuery,Ajax的使用变的越来越方便了,但是使用中还是会或多或少的出现一些让人短时间内痛苦的问题.本文暂时总结一些在使用JQuery Ajax中应该注意的问题,如有不恰当或者不完善的地方 ...
- Mongodb 与sql 语句对照
此处用mysql中的sql语句做例子,C# 驱动用的是samus,也就是上文中介绍的第一种. 引入项目MongoDB.dll //创建Mongo连接 var mongo = new Mongo(&qu ...
- Python 解压缩Zip和Rar文件到指定目录
#__author__ = 'Joker'# -*- coding:utf-8 -*-import urllibimport osimport os.pathimport zipfilefrom zi ...
- hadoop程序MapReduce之MaxTemperature
需求:求每年当中最高的温度 样本:temp.log 2016080623 2016072330 2015030420 输出结果:2016 30 2015 20 MapReduce分析设计: Mappe ...
- 开源免费天气预报接口API以及全国所有地区代码[值得收藏]
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...