【优先队列+贪心】POJ2431-Expedition
解题方法详见《挑战程序设计竞赛(第二版)》P74-75。注意首先要对加油站以位置为关键字快排,不要遗忘把终点视作一个加油量为0的加油站,否则最终只能到达终点前的加油站。
/*优先队列*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=+; struct rec
{
int pos,oil;
bool operator < (const rec& x) const
{
return pos<x.pos;
}
}; rec sta[MAXN];
int n,L,P,a,t=; void input()
{
scanf("%d",&n);
for (int i=;i<n;i++) scanf("%d%d",&sta[i].pos,&sta[i].oil);
scanf("%d%d",&L,&P);
for (int i=;i<n;i++) sta[i].pos=L-sta[i].pos;
sta[n].pos=L;//把终点也作为加油站
sta[n].oil=;
n++;
} void doit()
{
priority_queue<int> pque;
sort(sta,sta+n);
int npos=;
for (int i=;i<n;i++)
{
int d=sta[i].pos-npos;
while (P-d<)
{
if (pque.empty())
{
cout<<-<<endl;
return;
}
t++;
P+=pque.top();
pque.pop();
}
P-=d;
npos=sta[i].pos;
pque.push(sta[i].oil);
}
cout<<t<<endl;
return;
} int main()
{
input();
doit();
return ;
}
【优先队列+贪心】POJ2431-Expedition的更多相关文章
- POJ2431 优先队列+贪心 - biaobiao88
以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...
- 最高的奖励 - 优先队列&贪心 / 并查集
题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...
- hdu3438 Buy and Resell(优先队列+贪心)
Buy and Resell Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- H - Expedition 优先队列 贪心
来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...
- POJ2431 Expedition(排序+优先队列)
思路:先把加油站按升序排列. 在经过加油站时.往优先队列里增加B[i].(每经过一个加油站时,预存储一下油量) 当油箱空时:1.假设队列为空(能够理解成预存储的油量),则无法到达下一个加油站,更无法到 ...
- poj2431(优先队列+贪心)
题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
- POJ2431 Expedition 贪心
正解:模拟费用流 解题报告: 先放个传送门鸭,题目大意可以点Descriptions的第二个切换成中文翻译 然后为了方便表述,这里强行改一下题意(问题是一样的只是表述不一样辣,,, 就是说现在在高速公 ...
- poj2431 Expedition优先队列
Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Bein ...
- ZOJ-3410Layton's Escape(优先队列+贪心)
Layton's Escape Time Limit: 2 Seconds Memory Limit: 65536 KB Professor Layton is a renowned arc ...
随机推荐
- 通过or注入py脚本
代码思路 1.主要还是参考了别人的代码,确实自己写的和别人写的出路很大,主要归咎还是自己代码能力待提高吧. 2.将功能集合成一个函数,然后通过*args这个小技巧去调用.函数的参数不是argv的值,但 ...
- deepin安装metasploit
[1]安装metasploit 1.curl https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/tem ...
- C++学习之路(六):实现一个String类
直接贴代码吧,这段时间准备面试也正好练习了一下. class String { public: String(const char *str = ""); ~String(void ...
- python基础===pendulum '''Python datetimes made easy.'''
https://pypi.python.org/pypi/pendulum Pendulum的一大优势是内嵌式取代Python的datetime类,可以轻易地将它整合进已有代码,并且只在需要的时候才进 ...
- sunos kernel src
https://github.com/eocallaghan/AuroraUX-SunOS https://github.com/zoyanhui/coroutine-libtask https:// ...
- 3D Studio Max [www]
https://github.com/RealityFactory/Exporters https://github.com/code-google-com/3ds-max-dev https://g ...
- BZOJ 4241: 历史研究——莫队 二叉堆
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4241 题意:N个int范围内的数,M次询问一个区间最大的(数字*出现次数)(加权众数),可以 ...
- JDK1.8特性实现jdk动态代理,使业务解耦
首先,先创建一个interface IHello 目标接口类 interface IHello { void sayHello(); } 然后再写一个目标类的实现类 class HelloImpl i ...
- Leetcode 之Flatten Binary Tree to Linked List(50)
将左子树接到右子树之前,递归解决 void flatten(TreeNode *root) { if (root == nullptr)return; flatten(root->left); ...
- initWithFrame和initWithCoder的区别
如果使用了Interface Builder 方式或nib,就不会调用initWithFrame方法,因为nib文件知道怎么初始化了, 但可以使用initWithCoder这一个更深层的init方法来 ...