POJ2431---Expedition(加油问题,优先队列)

主要考察优先队列,思路:
1.以汽车为原地,建立一维坐标系。对加油站的坐标从小到大排序。
2.经过一个加油站,就把加油站的油量压入优先队列中,没油的时候就取出一个最大的,队列为空的时候意味着不能到达终点。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<functional>
#include<queue>
#define MAX 10005
using namespace std;
struct node {
    int stop;
    int fuel;
};
node a[MAX];
bool com(node a, node b)
{
    return a.stop < b.stop;
}
int main()
{
    int N; cin >> N;
    for (int i = 0; i < N; i++)
        cin >> a[i].stop >> a[i].fuel;
    int L, P;
    cin >> L >> P;
    for (int i = 0; i < N; i++)
        a[i].stop = L - a[i].stop;//以车起点为原点
    a[N].stop = L;//终点坐标
    sort(a, a + N, com);//对加油站坐标进行排序
    priority_queue<int>que;
    int pos = 0, tank = P, ans = 0;
    for (int i = 0; i <= N; i++)
    {
        int d = a[i].stop - pos;//接下来要走的距离
        while (tank < d)
        {
            if (que.empty())
            {
                cout << -1; return 0;
            }
            tank += que.top(); que.pop();
            ans++;
        }
        pos = a[i].stop;
        tank -= d;
        que.push(a[i].fuel);
    }
    cout << ans;
    system("pause");

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值