题目链接: 传送门

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(优先队列、贪心)的更多相关文章

  1. POJ 2431 Expedition【贪心】

    题意: 卡车每走一个单元消耗一升汽油,中途有加油站,可以进行加油,问能否到达终点,求最少加油次数. 分析: 优先队列+贪心 代码: #include<iostream> #include& ...

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

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

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

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

  4. POJ 2431 Expedition(探险)

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

  5. poj 3431 Expedition 优先队列

    poj 3431 Expedition 优先队列 题目链接: http://poj.org/problem?id=2431 思路: 优先队列.对于一段能够达到的距离,优先选择其中能够加油最多的站点,这 ...

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

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

  7. poj 2431 【优先队列】

    poj 2431 Description A group of cows grabbed a truck and ventured on an expedition deep into the jun ...

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

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

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

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

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

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

随机推荐

  1. java并发:线程池、饱和策略、定制、扩展

    一.序言 当我们需要使用线程的时候,我们可以新建一个线程,然后显式调用线程的start()方法,这样实现起来非常简便,但在某些场景下存在缺陷:如果需要同时执行多个任务(即并发的线程数量很多),频繁地创 ...

  2. Android子线程真的不能更新UI么

    Android单线程模型是这样描述的: Android UI操作并不是线程安全的,并且这些操作必须在UI线程执行 如果在其它线程访问UI线程,Android提供了以下的方式: Activity.run ...

  3. Bootstrap系列 -- 44. 分页导航

    带页码的分页导航,可能是最常见的一种分页导航,特别是在列表页内容超多的时候,会给用户提供分页的导航方式.平时很多同学喜欢用div>a和div>span结构来制作带页码的分页导航.不过,在B ...

  4. Linux 使用 iptables屏蔽IP段

    netfilter/iptables IP 信息包过滤系统是一种功能强大的工具,可用于添加.编辑和除去规则,这些规则是在做信息包过滤决定时,防火墙所遵循和组成的规则.这些规则存储在专用的信息包过滤表中 ...

  5. Shell脚本编程中的几个问题

    条件语句 正确的写法: if [ $1 = "-f" ] #注意这里,前后方括号和中间的内容之间必须有空格! then commands fi 错误的写法: if [$1 == & ...

  6. 《Android性能优化》学习笔记链接<转载>

    今天找到一博文汇总了 Android性能优化 比较好的文章 ,本计划全看完,自己再精简下,因篇幅太长,先收藏了,等有时间 再仔细拜读,总结自己的看法:  第一季: http://www.csdn.ne ...

  7. springMVC+mybatis 进行单元测试时 main SqlSessionFactoryBean - Parsed configuration file: 'class path resource' 无限的读取xml文件

    今天终于写完的Dao层的操作,怀着无比激动的心情,进行单元测试,就在最后一个方法,对的就是最后一个方法,启动单元测试就会报以下错误: [2016-05-11 18:25:01,691] [WARN ] ...

  8. javascript 方法实例

    输出对象的属性名称与值 : boj(o){ for(var p in o){ console.log(p + ":" + o[p] + "\n"); } } 构 ...

  9. Mysql-通过case..when实现oracle decode()函数进行多值多结果判断

    oracle的decode函数使用:http://www.cnblogs.com/hwaggLee/p/5335967.html case ..when 函数使用:http://www.cnblogs ...

  10. linux下mysql基本命令

    1, 创建mysqld数据库的管理用户:             要把root用户设置为管理员,我们应该运行下面的命令:    # mysqladmin -u root password 一般情况下, ...