PAT 1033 To Fill or Not to Fill

题目链接


  • 把终点视为单位油价为0,里起点距离为D的加油站,起哨兵作用
  • 假设当前所处加油站编号为now, 接下来从满油状态下能到达的所有加油站中选出下一个前往的加油站:
    1. 可以找到油价比当前加油站低的,加恰好能到达该加油站的油
    2. 找不到比当前低的,去最低的。先在当前加油站加满
    3. 找不到加油站,则最远能到达的距离即为当前加油站距离,加上满油到达的最远距离
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 500 + 5;
const int INF = (1 << 30) - 1;
struct station {
    double price;
    double dis;
} sta[maxn];

inline bool cmp(station a, station b)
{
    return a.dis <= b.dis;
}

int main()
{
    double cap, dis, davg;
    int n;
    scanf("%lf%lf%lf%d", &cap, &dis, &davg, &n);
    for (int i = 0; i < n; ++i) {
        scanf("%lf%lf", &sta[i].price, &sta[i].dis);
    }
    sort(sta, sta + n, cmp);
    sta[n].price = 0, sta[n].dis = dis;
    if (sta[0].dis != 0) {
        printf("The maximum travel distance = 0.00");
    }
    else
    {
        double total = 0, maxdis = cap * davg, nowtank = 0;
        int now = 0;
        while (now < n) {
            int p = -1;
            double minprice = INF;
            for (int i = now + 1; i <= n && sta[i].dis - sta[now].dis <= maxdis; ++i) {
                if (sta[i].price < minprice) {
                    minprice = sta[i].price;
                    p = i;
                }
                if (minprice < sta[now].price)
                    break;
            }

            if (p == -1) break;
            double need = (sta[p].dis - sta[now].dis) / davg;
            if (minprice < sta[now].price) {
                if (nowtank < need) {
                    total += (need - nowtank) * sta[now].price;
                    nowtank = 0;
                }
                else {
                    nowtank -= need;
                }
            }
            else {
                total += (cap - nowtank) * sta[now].price;
                nowtank = cap - need;
            }
            now = p;
        }
        if (now == n) {
            printf("%.2lf", total);
        }
        else {
            printf("The maximum travel distance = %.2lf", sta[now].dis + maxdis);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值