PAT1 1033 To Fill or Not to Fill

题目链接
我的github

题目大意

起点到终点有一段距离,且路上有一些加油站,每个加油站有自己的油价,现在要求汽车从起点到终点所需要的最少油钱是多少。假设汽车一开始没油

输入

每组包含一个测试用例。每个用例的第一行是四个正数, C m a x ≤ 100 C_{max} \leq 100 Cmax100表示汽车油箱能容纳油的最大单位数, D ≤ 30000 D\leq30000 D30000表示起点到终点的距离, D a v g ≤ 20 D_{avg} \leq 20 Davg20表示每单位的油可以走的距离, N ≤ 500 N\leq500 N500表示加油站的数量。之后会有 N N N行,每行油两个非负数表示加油站的信息。 P i P_i Pi表示油价, D i ≤ D D_i \leq D DiD

输出

对每个用例,在一行中输出从起点到终点最小的油钱,精确到小数点后两位。如果不能到达终点就输出The maximum travel distance = XX是汽车能走的最远距离,精确到小数点后两位

样例输入

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

50 1300 12 2
7.10 0
7.00 600

样例输出

749.17

The maximum travel distance = 1200.00

解析

这题可以用贪心算法

  • 先计算当前可以走的最大距离,然后有多少个加油站在这个距离内
  • 看是不是所有的加油站的油价都比现在的高
    • 如果是就先判断是否终点可达
      • 如果是,就把油加至刚好能到终点
      • 如果不是,在当前加油站把油加满,然后去能去的站里油价最少的
    • 如果不是,就找到最近的油价比当前低的站,然后在当前站里把油加至刚好能到那个站
  • 循环以上步骤,直到到达终点
# -*- coding: utf-8 -*- 
# @Time : 2019/5/29 18:52 
# @Author : ValarMorghulis 
# @File : 1033.py
def solve():
    capacity, distance, per, total = map(float, input().split())
    total = int(total)
    stations = list()
    flag = False  # 表示起点是否有加油站
    for i in range(total):
        price, length = map(float, input().split())
        if length == 0.0:
            flag = True
        stations.append([length, price])
    if not flag:
        print("The maximum travel distance = 0.00")
        return
    stations = sorted(stations, key=lambda x: x[0])
    finalPrice = stations[-1][1]
    stations.append([distance, finalPrice])  # 将终点设一个加油站,油价就为它前一个的油价
    pos, cost, now = 0, 0.0, 0.0  # 当前站,花费,当前油量
    while pos < len(stations) - 1:  # 如果当前不是终点
        maxDis = capacity * per + stations[pos][0]  # 计算当前能走的最大距离
        arr = [x for x in range(pos + 1, len(stations)) if stations[pos][0] < stations[x][0] <= maxDis]  # 找出可达的站
        if len(arr) == 0:  # 如果都不可达说明终点不可达
            print("The maximum travel distance = %.2f" % maxDis)
            return
        mark = pos  # 标记下一个应去的站
        nearTheEnd = False  # 终点是否可达
        for i in arr:
            if stations[i][0] == distance:
                nearTheEnd = True
            if stations[i][1] <= stations[mark][1]:
                mark = i
                break
        if mark != pos:
            cost = cost + ((stations[mark][0] - stations[pos][0]) / per - now) * stations[pos][1]
            now = 0
            pos = mark
        else:
            if nearTheEnd:
                cost = cost + ((distance - stations[pos][0]) / per - now) * stations[pos][1]
                pos = len(stations) - 1
                now = 0
            else:
                cost = cost + (capacity - now) * stations[pos][1]
                mark += 1
                for i in arr:
                    if stations[i][1] <= stations[mark][1]:
                        mark = i
                now = capacity - (stations[mark][0] - stations[pos][0]) / per
                pos = mark
    print("%.2f" % cost)


if __name__ == "__main__":
    solve()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值