[Python](PAT)1033 To Fill or Not to Fill (25 分)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: C​max​​ (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; D​avg​​ (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P​i​​, the unit gas price, and D​i​​ (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

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

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00

题目大意

有了公路,从杭州开车到可以开车到任意城市。但是由于汽车的油箱容量有限,不得不在路上找到加油站加油,不同的加油站可能会给出不同的价格。求出从杭州出发到目的距离的最少花费。如果到不了目的距离,则输出汽车最远可以到哪里。

分析

使用贪心算法。

首先搜索从当前位置出发,加满油是否有可以到达的加油站,如果没有,则输出最远可以到达的位置。如果有可以到达的加油站,则判断其中是否有价格比当前加油站更便宜的加油站。如果有,则在当前加油站加油到刚好可以到达第一个价格比当前加油站低的加油站(确保了后续的距离使用的是更加便宜的汽油)。如果没有价格比当前加油站低的加油站,则在当前加油站加满,行驶到可以行驶到的价格最低的加油站(确保使用完当前的油之后,使用的是比较便宜的油)。

在终点设置一个价格为0的加油站,确保最后汽车开到终点,并且价格最低。

使用station存储加油站的信息。使用contain存储当前拥有的汽油量,使用now存储当前里程数,使用price存储当前花费。

注意!有一个测试点是起点没有加油站的,所以,要先检测0处是否有加油站,没有就直接输出结果!这个测试点,让我十分怨念。

每次循环时,先判断当前是否在终点,如果是,则输出。如果否,则使用longest变量存储最远到达位置,使用temp数组存储能够到达的加油站。如果能到达的加油站数量为0,则输出。否则检查这些加油站中是否有价格比当前低的,如果有,则开往第一个价格低的加油站。如果没有,则在当前位置加满油,然后去这些加油站中价格最低的加油站。

最后输出结果就可以了。

python实现

def main():
    line = input().split(" ")
    capacity, dis, per, n = (int(x) for x in line)
    station = {}
    for x in range(n):
        line = input().split(" ")
        price, length = float(line[0]), int(line[1])
        station[length] = price
    station[dis] = 0
    contain, now, price = 0, 0, 0
    if 0 not in station:
        print('The maximum travel distance = 0.00')
    while(0 in station):
        if now == dis:
            print('{:.2f}'.format(price))
            break
        longest = int(now + capacity * per)
        temp = sorted([x for x in station if x > now and x <= longest])
        if len(temp) > 0:
            arri = sorted([[x, station[x]] for x in temp], key = lambda x : x[1])
            b = sorted([x for x in arri if x[1] <= station[now]])
            if len(b) == 0:
                target = arri[0][0]
                price = price +  (capacity - contain) * station[now]
                contain = capacity - (target - now) / per
                now = target
            else:
                target = b[0][0]
                need = (target - now) / per
                price += (need-contain) * station[now]
                contain = 0
                now = target
        else:
            result='The maximum travel distance = {:.2f}'.format(longest)
            print(result)
            break

if __name__ =="__main__":
    main()

 python解PAT甲级,答案都在这了

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值