【优先队列+贪心】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 ...
随机推荐
- 初识费用流 模板(spfa+slf优化) 餐巾计划问题
今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...
- Canvas 高级
一.Canvas 高级 1.变换--位移 translate(x, y) 2.变换-缩放 scale(xS, yS) 3.变换-旋转 rotate(弧度) 4.环境的保存和释放 save() rest ...
- 解决: httpclient ssl 验证导致死锁问题
线上图片下载服务器平时运行正常,最近突然出现一种比较奇怪的现象,只接受请求,但却没有处理请求,最开始怀疑下载线程挂掉了,dump 项目线程后发现异常: "pool-2-thread-1&qu ...
- Nginx-1.6.3反向代理
源码安装nginx cat /etc/redhat-release uname -rm yum install pcre-devel openssl-devel -y rpm -qa pcre pcr ...
- windows server 2012 IIS配置之FTP站点
原文地址:[原创]winserver2012IIS配置之FTP站点作者:hkmysterious 一.实验拓扑: 使server2012客户计算机通过ftp方式从FTP服务器上下载已上传并共享的文 ...
- spring使用aop需要的jar包,和常见异常
3.0以后spring不再一起发布aop依赖包,需要自己导入: 必须包: 这几个jar包分别为 1.org.springframework.aop-3.1.1.RELEASE 这个是spring的 ...
- linux命令(37):locate命令
1.命令格式: Locate [选择参数] [样式] 2.命令功能: locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期 ...
- highcharts 折线,饼状,条状综合图
完整代码如下: <head> <meta http-equiv="Content-Type" content="text/html; charset=u ...
- docker容器中文件的上传与下载
原文地址:传送门 1.上传文件 docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH [OPTIONS]:保持源目标中的链接,例: docker cp ...
- Centos7 Elasticsearch部署
(1)ELKStack简介 1.elk介绍 ELK Stack包含:ElasticSearch.Logstash.Kibana ElasticSearch是一个搜索引擎,用来搜索.分析.存储日志.它是 ...