题目链接: 传送门

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. 使用 data-* 属性来嵌入自定义数据

    1. HTML 实例 <ul> <li data-animal-type="bird">Owl</li> <li data-animal- ...

  2. PHP中JSON的跨域调用

    主调文件index.html <script type="text/javascript"> function getProfile(str) { var arr = ...

  3. Windows Phone 8 下载文件进度

    后台代码: public partial class MainPage : PhoneApplicationPage { private long siz; private long speed; p ...

  4. js前端的各种面试题

    转载:http://bbs.blueidea.com/thread-3107428-1-1.html 1.截取字符串abcdefg的efg //alert('abcdefg'.substring(4) ...

  5. TF2ZP函数

    TF2ZP 中TF是什么意思?   Transfer function   tf 就是传递函数的意思,简称传函 tf2zp是将传递函数转换为零极点形式的一个转换函数   [Z,P,K] = TF2ZP ...

  6. 自然数从1到n之间,有多少个数字含有1

        问题明确而简单.for循环肯定是不好的.       用递推方法:       定义h(n)=从1到9999.....9999  ( n 个 9)之间含有1的数字的个数.定义f(n)为n位数中 ...

  7. RHCSA试题分享

    RHCSA 部分 密码破解 在linux16最后加入 rd.break 进入交换模式以后输入下列指令: mount  -o remount,rw /sysroot chroot /sysroot pa ...

  8. 【Alpha版本】冲刺阶段——Day 6

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  9. 延时程序执行Qt

    有时候为了让程序暂停一下,不让它一直跑下去,可以使它进入循环结构中! 例如: #include <QCoreApplication> #include <qdebug.h> # ...

  10. jenkins 把包传到远程服务器上

    首先我们在 一台服务器上部署svn,maven,jdk,tomcat,nexus,还有Jenkins. 这里我主要记录Jenkins. 首先我们从网上下载Jenkins的包 wget http://m ...