贪心_1033 To Fill or Not to Fill (25 分)

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(所有费用最小),也就可以表示为,在到达终点的时候,车一定会停下来加油的,那么整个贪心的过程就开始了

一、首先当车程范围内有比开始加油费用更小的,那么就停下来加油

二、如果车程范围内没有比开始加油费更小的,那么就在开始加油的位置加满油,然后再车程范围内经过的加油站中找加油费用最小的加油站加油,那么在这个加油站加多少油,要根据后面的费用计算,也就是在遍历后面的节点的时候,还可能出现情况一、情况二,或者情况三,需要要向后继续讨论

三、车程范围内没有加油站了,那么就不可能到达终点,直接输出最大情况即可

样例三为:开始没有加油站选择,所以直接输出最大距离0即可,样例卡了好久好久

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include <map>
#include <set>
#define INF 0x3f3f3f3f

using namespace std;
const int maxn = 505;
int c,d,da,n;
struct Station
{
    double p;
    int dis;
    bool operator < (const Station &a )const
    {
        return dis<a.dis;
    }
}sta[maxn];

int main()
{
    scanf("%d%d%d%d",&c,&d,&da,&n);
    for(int i = 0;i < n;i ++)
        scanf("%lf%d",&sta[i].p,&sta[i].dis);
    sta[n].dis = d;sta[n].p = 0;
    sort(sta,sta+n+1);
    if(sta[0].dis != 0)
    {
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    int dis = 0,ppos = 0,mdis = da * c,pdis = 0;double ans = 0;
    for(int i = 1;i <= n;i ++)
    {
        if(sta[i].dis - sta[ppos].dis <= mdis && sta[i].p < sta[ppos].p)   //600米范围内存在加油站并且价格小于之前加油的价格
        {
            ans += (sta[i].dis - pdis) * sta[ppos].p / da;
            ppos = i;pdis = sta[ppos].dis;
        }
        else if(sta[i].dis - sta[ppos].dis > mdis && sta[i].dis - sta[i-1].dis <= mdis)   //中间存在加油站,但是没有比上一个加油站便宜的加油站
        {
            ans += sta[ppos].p * (mdis - pdis + sta[ppos].dis)/ da;
            int x = ppos;
            pdis = sta[ppos].dis + mdis;
            double m = sta[ppos + 1].p;
            for(int j = ppos + 1;j < i;j ++)
                if(sta[j].p <= m)
                    ppos = j,m = sta[j].p;
            i -- ;
        }
        if(sta[i].dis - sta[ppos].dis > mdis && sta[i].dis - sta[i-1].dis > mdis)  //600米范围内不存在加油站,所以到达的最远距离已经确定
        {
            printf("The maximum travel distance = %.2lf\n",(double)pdis + mdis);
            return 0;
        }
    }
    printf("%.2lf\n",ans);
    return 0;

}

猜你喜欢

转载自blog.csdn.net/li1615882553/article/details/84865134