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; Davg (≤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

分析:
1、贪心模拟,恶心恶心。测试点2错误是如果出发点没有加油战的情况(特判)

2、两种情况:一、在汽车能到达的最大距离内,如果出现下一个加油站的价格比目前所在的加油站价格低,则只加刚好到达比目前便宜的加油站的油量。二、在汽车能到达的最大距离内,如果没出现比目前所在加油战油价低的加油站,则在目前加油站加满,到达(除目前加油站)下一个最大距离范围内的价格最低的加油站

3、为了计算最后一段路程我们可以把目的地设一个加油站价格为0,这样到达目的地前几站且能直接到达目的地时,可以直接按第一种情况直接到达目的地(方便)

我的代码:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 510;
struct Gas//加油站
{
    double price,distance;
}gas[maxn];
struct Car//车
{
    double cost,distance,nowstore,maxstore;
}car;
int cmax,d,davg,n,i = 0;
bool cmp(Gas g1,Gas g2)
{
    return g1.distance < g2.distance;
}
int getmin(int i)//按情况得到最小价格的加油站序号
{
    int k = i + 1,minid = i,temp = i + 1;
    while (k <= n && car.maxstore * davg + gas[i].distance >= gas[k].distance)
    {
        if (gas[k].price < gas[minid].price)
        {
            minid = k;
            break;
        }
        if (gas[k].price < gas[temp].price)
            temp = k;                    
        k++;
    }
    if (minid == i)
        minid = temp;
    return minid;
}
int main()
{
    scanf("%d%d%d%d",&cmax,&d,&davg,&n);
    car.maxstore = cmax;
    for (int i = 0 ;i < n ;i++)
    {
        double price,distance;
        scanf("%lf%lf",&price,&distance);
        gas[i].distance = distance;
        gas[i].price = price;
    }
    gas[n].distance = d;
    gas[n].price = 0;
    sort(gas,gas+n,cmp);
    if (gas[0].distance != 0)
        printf("The maximum travel distance = 0.00");
    else
    {
        for (i = 0 ;i < n ;i++)
        {
            int minid = getmin(i);
            if (car.maxstore * davg + gas[i].distance >= gas[minid].distance && gas[minid].price < gas[i].price)
            {
                car.cost += gas[i].price * ((gas[minid].distance - gas[i].distance) / davg - car.nowstore);
                car.distance += gas[minid].distance - gas[i].distance;
                car.nowstore = 0;
            }
            else
            {
                car.cost += (car.maxstore - car.nowstore) * gas[i].price;        
                if (gas[i+1].distance - gas[i].distance <= car.maxstore * davg)
                    car.distance += gas[minid].distance - gas[i].distance;
                else
                {
                    car.distance += car.maxstore * davg;
                    printf("The maximum travel distance = %.2f",car.distance);
                    break;
                }
                car.nowstore = car.maxstore - (gas[minid].distance - gas[i].distance) / davg;
            }
            i = minid - 1;
        }        
    }
    if (i == n)
        printf("%.2f\n",car.cost);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

情书、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值