【PAT】【Advanced Level】1033. To Fill or Not to Fill (25)

4 篇文章 0 订阅

1033. To Fill or Not to Fill (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

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: Cmax (<= 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: Pi, the unit gas price, and Di (<=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
原题链接:

https://www.patest.cn/contests/pat-a-practise/1033

思路:

每一段路上尽可能使用从较便宜的加油站所加的油

每一个加油站的油可供当前距离加(邮箱距离*单位油支撑距离)这些路程的使用

按价格从高到低排列加油站

对于每一个加油站,把自己能供应到的地方标注到路上,进行覆盖

由于价格从高到低处理,所以在价格高、价格低的加油站同时能供应到的地方,价格低的加油站可以覆盖掉价格高的加油站。

沿路走一趟

如果某个地方不能被任何加油站覆盖,则说明最多能走到这个地方

否则总花费加上当前段的花费,一直处理完全程

CODE:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define N 501
#define D 30010
using namespace std;
typedef struct S
{
    double per;
    int dis;
};
S gas[N];
int fi[D];
bool cmp(S a,S b)
{
    return a.per>b.per;
}
int main()
{
    memset(fi,-1,sizeof(fi));
    int cmax,len,aver,n;
    scanf("%d%d%d%d",&cmax,&len,&aver,&n);
    for (int i=0;i<n;i++)
        scanf("%lf %d",&gas[i].per,&gas[i].dis);
    sort(gas,gas+n,cmp);
    for (int i=0;i<n;i++)
    {
        for (int j=gas[i].dis;j<min(len,gas[i].dis+cmax*aver);j++)
        {
            fi[j]=i;
        }
    }
    int fl=0;
    int md=0;
    double tol=0;
    for (int i=0;i<len;i++)
    {
        if (fi[i]==-1)
        {
            fl=1;
            md=i;
            break;
        }
        else
        {
            tol=tol+gas[fi[i]].per;
        }
    }
    if (fl==1)
    {
        printf("The maximum travel distance = %.2lf",(double)md);
    }
    else
    {
        tol=tol/(double)aver;
        printf("%.2lf",tol);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值