题目链接: 传送门

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. 大数据下多流形聚类分析之谱聚类SC

    大数据,人人都说大数据:类似于人人都知道黄晓明跟AB结婚一样,那么什么是大数据?对不起,作为一个本科还没毕业的小白实在是无法回答这个问题.我只知道目前研究的是高维,分布在n远远大于2的欧式空间的数据如 ...

  2. PRML读书会第六章 Kernel Methods(核函数,线性回归的Dual Representations,高斯过程 ,Gaussian Processes)

    主讲人 网络上的尼采 (新浪微博:@Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:16:05 今天的主要内容:Kernel的基本知识,高斯过程.边思考边打字,有点慢, ...

  3. 架构系列:ASP.NET 项目结构搭建

    我们头开始,从简单的单项目解决方案,逐步添加业务逻辑的约束,从应用逻辑和领域逻辑两方面考虑,从简单的单个项目逐步搭建一个多项目的解决方案.主要内容:(1)搭建应用逻辑和领域逻辑都简单的单项目 (2)为 ...

  4. 手把手教你在Windows下搭建React Native Android开发环境

    最近看到React Native好像好厉害的样子,好奇心驱使之下体验了一下并将在Window下搭建React Natvie Android环境的步骤记录下来,并有需要的朋友参考.(我都是参考官方文档的 ...

  5. 备忘:maven 错误信息: Plugin execution not covered by lifecycle configuration

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. js的Array的map和sort实现方法

    Array.prototype.mapA = function(fun /*, thisp*/) { var len = this.length; if (typeof fun != "fu ...

  7. Codeforces Round #370(div 2)

    A B C :=w= D:两个人得分互不影响很关键 一种是f[i][j]表示前i轮,分差为j的方案数 明显有f[i][j]=f[i-1][j-2k]+2*f[i-1][j-2k+1]+...+(2k+ ...

  8. mysql忘记密码怎么办?

    mysql有时候忘记密码了怎么办?我给出案例和说明!一下就解决了! Windows下的实际操作如下 1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysql ...

  9. Ubuntu's Trash

    1.Location    Where is Trash?    /home/userName/.local/share/Trash2.Under Trash    Three files:      ...

  10. C++ new失败的处理

    我们都知道,使用 malloc/calloc 等分配内存的函数时,一定要检查其返回值是否为“空指针”(亦即检查分配内存的操作是否成功),这是良好的编程习惯,也是编写可靠程序所必需的.但是,如果你简单地 ...