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

#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. NSdate 时间格式

    NSdate 时间格式 NSTimeInterval 时间间隔 基本单位 秒 NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象 日期格式如下: y  年 M  年 ...

  2. Android运行异常情况分析(持续更新)

    1.java.lang.IllegalAccessException: access to class not allowed 原因:在写class 文件的时候没有把class设置成public 2. ...

  3. jquery自适应布局

    代码整理 - uix.layout.js /** * Grace [jQuery.js] * * UIX页面布局 * 290353142@qq.com * exp: * $.uix.layout(); ...

  4. 反射(学习整理)----Class类和加载器ClassLoader类的整理

    1.学习反射的时整理的笔记!Class类和ClassLoader类的简单介绍 反射机制中的Class Class内部到底有什么呢?看下图! 代码: Class cls=Person.class; .C ...

  5. 支持H5的一般设置

    1.因为IE8既不支持HTML5也不支持CSS3 Media,所以我们需要加载两个JS文件,来保证我们的代码实现兼容效果: <script src="https://oss.maxcd ...

  6. javascript 学习笔记之面向对象编程(一):类的实现

    ~~想是一回事,做是一回事,写出来又是一回事~~一直以来,从事C++更多的是VC++多一些,从面向过程到面向对象的转变,让我对OO的编程思想有些偏爱,将一个客观存在的规律抽象出来总是让人比较兴奋,通过 ...

  7. C#程序中:如何删除xml文件中的节点、元素。

    C#中动态的清理xml文件中的垃圾信息是程序员必会的哦.这就像数据库一样,不会清理数据怎么可以呢?其实xml文件就可以用作一个小的数据库,存储一些简单的信息.所以,用C#程序实现xml文件的增.删.改 ...

  8. WPF 保存文件

    private void button2_Click(object sender, RoutedEventArgs e) { var saveFileDialog1 = new SaveFileDia ...

  9. jsp 页面通过jq处理默认 选中的项 数据是通过遍历显示

    jsp页面循环显示里面是<a></a>或者<input>  id 以什么开头的id,然后当你点击那个的时候就在那个上面添加样式 <div> <di ...

  10. ip输入框键入.或者合法数字自动选择下一个输入框效果

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...