简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e4+9;
int dist,p,n;
struct S
{
int d,f;
bool operator <(const S & xx) const
{
return d<xx.d;
}
}stop[maxn]; struct cmp
{
bool operator ()(const S &a,const S &b) const
{
return a.f<b.f;
}
}; int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d %d",&stop[i].d,&stop[i].f);
scanf("%d %d",&dist,&p);
for(int i=1;i<=n;i++)
stop[i].d=dist-stop[i].d;
sort(stop+1,stop+1+n); int ans=0,t=1;
bool flag=true;
priority_queue <S,vector<S>,cmp> q;
while(p<dist)
{
while(t<=n&&stop[t].d<=p) q.push(stop[t++]);
if(q.empty())
{
flag=false;
break;
}
ans++;
p+=q.top().f;
q.pop();
}
if(flag) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}

poj 2431 Expedition 贪心的更多相关文章

  1. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  2. poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2431 题解 朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪 ...

  3. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

  4. POJ 2431 Expedition 贪心 优先级队列

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Descr ...

  5. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  6. POJ 2431 Expedition (贪心 + 优先队列)

    题目链接:http://poj.org/problem?id=2431 题意:一辆卡车要行驶L单位距离,卡车上有P单位的汽油.一共有N个加油站,分别给出加油站距终点距离,及加油站可以加的油量.问卡车能 ...

  7. POJ 2431——Expedition(贪心,优先队列)

    链接:http://poj.org/problem?id=2431 题解 #include<iostream> #include<algorithm> #include< ...

  8. POJ 2431 Expedition(优先队列、贪心)

    题目链接: 传送门 Expedition Time Limit: 1000MS     Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...

  9. POJ 2431 Expedition (优先队列+贪心)

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

随机推荐

  1. WTL的消息机制

    Windows消息 众所周知,Windows消息有两种:队列话消息和非队列话消息.队列话消息是被Windows操作系统放入消息队列的,程序通过主消息循环不断的从消息队列中取出消息并分发到各自的窗体调用 ...

  2. GUI编程笔记(java)10:GUI实现一级菜单

    1.首先:菜单组件 MenuBar,Menu,MenuItem              先创建菜单条,再创建菜单,每一个菜单中建立菜单项.              也可以菜单添加到菜单中,作为子菜 ...

  3. 安装指南:Win10下安装CentOs7

    系统安装 安装准备 系统:CentOS 7.Win 10 硬件:U盘一枚.PC一台 软件:UltraISO 安装步骤 使用UltraISO将镜像写入U盘 window10使用磁盘管理,空出一个未分配的 ...

  4. c# PadLeft,PadRight用法

    补位 string str = "100"; str.PadLeft(5,'0') 输出:00100 str.PadRight(5, '0') 输出:10000

  5. CSS中Position属性

    也许你看到这个标题觉得很简单,确实这是一篇关于CSS中Position属性基础知识的文章,但是关于Position的一些细节也许你不了解. 1.简介 position有五个属性: static | r ...

  6. VS2010打不开VS2012 .NET MVC 工程,及打开后部分模块加载不正确的解决办法

    转自http://www.xuebuyuan.com/2042634.html 首先,如果sln打开不正确,用(notepad++)打开sln 比如 VS2010的前两行为: Microsoft Vi ...

  7. [C# 基础知识系列]专题四:事件揭秘

    转自http://www.cnblogs.com/zhili/archive/2012/10/27/Event.html 引言: 前面几个专题对委托进行了详细的介绍的,然后我们在编写代码过程中经常会听 ...

  8. 学习java随笔第四篇:运算符

    算术运算符 "+":加法运算符,也可做字符连接用途 "-":减法运算符 "*":乘法运算符 "/":除法运算符 &quo ...

  9. Android切换页面--setContentView

    setContentView 一般切换页面,通过Intent,startActivity可以实现,但系统创建Activity是非常耗时的,如果对切换画面时间有要求,只能用setContentView在 ...

  10. Linux同步时间命令ntpdate

    转自:http://orgcent.com/linux-ntpdate/ 由于要同步Linux服务器的时间,为了保证时间的高度准确性,放弃date命令而转向ntpdate(同步时间命令). 方案如下: ...