POJ 2431 Expedition

http://poj.org/problem?id=2431

题意:驾驶一辆初始油量为P的车,路长L,一路有N个加油站,分别有油量为Ai,距终点Bi,每单位距离消耗1油量,问要到终点最少加油次数,无法到达终点输出-1

分析:当车走到没油的时候,优先选择之前油量最大的加油站加油(可多次),然后继续行驶,如果优先队列为空,则无法到达终点

将加油站距终点的距离转换为距起点的距离,并将终点看成距离L,油量0的加油站

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

#define INF 0x3F3F3F3F

typedef pair<int, int> P;

int main()
{
    // freopen("input.txt", "r", stdin);
    int n, l, p;
    cin >> n;
    P a[10010];
    for(int i = 0; i < n; i++)
        cin >> a[i].first >> a[i].second;
    cin >> l >> p;
    for(int i = 0; i < n; i++)
        a[i].first = l - a[i].first;
    sort(a, a+n);
    // for(int i = 0; i < n; i++)
    //  cout << a[i].first << endl;
    priority_queue<int> pri;
    a[n].first = l;
    a[n].second = 0;
    n++;
    int ans = 0, pos = 0, tank = p;
    for(int i = 0; i < n; i++)
    {
        int d = a[i].first - pos;
        while(tank - d < 0)
        {
            if(pri.empty())
            {
                cout << -1 << endl;
                return 0;
            }
            else
            {
                tank += pri.top();
                pri.pop();
                ans++;
            }
        }
        tank -= d;
        pos = a[i].first;
        pri.push(a[i].second);
    }
    cout << ans << endl;

    return 0;
}

 

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注


这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。