POJ 2431 Expedition(优先队列、贪心)
题目链接: 传送门
Expedition
Time Limit: 1000MS Memory Limit: 65536K
题目描述
驾驶一辆卡车行驶L单位距离。最开始有P单位的汽油。卡车每开1单位距离消耗1单位的汽油。在途中一共有N个加油站。假设卡车的燃料箱容量无限大,问如果到达终点最少的加油次数。
思路
在卡车开往终点途中,只有在加油站才可以加油,换做认为“在到达加油站i时,就获得了一次在之后任何时候都可以加油的权利”
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct oil{
int dis,fuel;
};
bool cmp(struct oil x,struct oil y)
{
return x.dis <= y.dis;
}
int main()
{
int N;
while (scanf("%d",&N) != EOF)
{
struct oil stops[10005];
priority_queue<int>pque;
int L,P;
for (int i = 0;i < N;i++)
{
scanf("%d%d",&stops[i].dis,&stops[i].fuel);
}
scanf("%d%d",&L,&P);
for (int i = 0;i < N;i++)
{
stops[i].dis = L - stops[i].dis;
}
sort(stops,stops + N,cmp);
int cnt = 0;
bool flag = true;
for (int i = 0;i < N;i++)
{
if (P >= L)
{
break;
}
if (P < stops[i].dis)
{
if (pque.empty())
{
flag = false;
break;
}
else
{
while (!pque.empty() && P < stops[i].dis)
{
cnt++;
int tmp = pque.top();
pque.pop();
P += tmp;
}
if (pque.empty() && P < stops[i].dis)
{
flag = false;
break;
}
pque.push(stops[i].fuel);
}
}
else
{
pque.push(stops[i].fuel);
}
}
if (P < L && !pque.empty())
{
int tmp = pque.top();
pque.pop();
P += tmp;
cnt++;
}
if (!flag)
{
printf("-1\n");
}
else
{
printf("%d\n",cnt);
}
}
return 0;
}
POJ 2431 Expedition(优先队列、贪心)的更多相关文章
- POJ 2431 Expedition【贪心】
题意: 卡车每走一个单元消耗一升汽油,中途有加油站,可以进行加油,问能否到达终点,求最少加油次数. 分析: 优先队列+贪心 代码: #include<iostream> #include& ...
- POJ 2431 Expedition (贪心 + 优先队列)
题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...
- POJ 2431——Expedition(贪心,优先队列)
链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- poj 3431 Expedition 优先队列
poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...
- POJ 2431 Expedition (贪心+优先队列)
题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...
- poj 2431 【优先队列】
poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...
- poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...
- poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!
Expedition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10025 Accepted: 2918 Descr ...
- POJ 2431 Expedition (优先队列+贪心)
题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...
随机推荐
- ZH奶酪:Java调用NLPIR汉语分词系统
NLPIR工具 支持自定义词表: 可以离线使用: 下载地址:http://ictclas.nlpir.org/newsdownloads?DocId=389 在线演示:http://ictclas.n ...
- Ext.NET-基础篇
概述 本文介绍Ext.NET的基本概念,安装配置.布局以及容器,最后介绍了DirectEvents.DirectMethod.Listener,并提供了示例代码. 示例代码下载地址>>&g ...
- js如何判断一个数组
typeof [] 为一个"object" 不能通过此方法判断一个数组 方法 1.instanceof方法,这个方法用的比较多. 2.这个是es5以后推荐的方法,Object.pr ...
- pay-as-you-go
What is pay as you go? A pay as you go deal means you aren’t tied into a contract and can top up you ...
- android之拍照与摄像
拍照和摄像的意图很简答,这里直接贴代码 布局文件 <?xml version="1.0" encoding="utf-8"?> <Linear ...
- 【JavaEE企业应用实战学习记录】optiontransferselect实现两个列表选择框
<%@ page contentType="text/html; charset=GBK" language="java"%> <%@tagl ...
- 转 漫谈linux文件IO
在Linux 开发中,有几个关系到性能的东西,技术人员非常关注:进程,CPU,MEM,网络IO,磁盘IO.本篇文件打算详细全面,深入浅出.剖析文件IO的细节.从多个角度探索如何提高IO性能.本文尽量用 ...
- nginx 的动静分离配置(tomcat)
nginx+tomcat是想动静分离配置 首先在nginx的配置文件中添加tomcat的的集群配置 upstream tomcats { ip_hash; server 192.168.0.251:8 ...
- [转]Filter实现处理中文乱码,转义html标签,过滤敏感词
原文地址:http://www.cnblogs.com/xdp-gacl/p/3952405.html 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可 ...
- linux命令巧用,随手记
巧用1 一句话修改或者给用户设置密码,并且防止历史命令记住设置的密码 echo "qqxde:ioj123"|passwd --stdin Dicky && his ...