1033. To Fill or Not to Fill (25) (贪心好题)

来源:互联网 发布:小天才电话手表软件 编辑:程序博客网 时间:2024/06/05 09:54

1033. To Fill or Not to Fill (25)

时间限制
10 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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 27.10 07.00 600
Sample Output 2:
The maximum travel distance = 1200.00

题意:给定n个加油站,给出每个加油站的加油的价格,与原点的距离,并且给出汽车油箱的容量,以及每升汽油能跑多远,问能否到达给定的终点, 如果不能, 输出汽车最多能跑多远

题解:

贪心好题!

首先,我们肯定要按照距离,将这n个加油站排序

下面我们就开始贪心了

我们首先找出从当前加油站到能够到达的下一个比当前加油站便宜的站点

1> 如果能找到,就可以直接跳到那一个站点

       (1)如果此时油箱里的油足够, 就不需要加油了

       (2)如果不够,然后冲恰好到那一点的油

2>如果找不到,判断能不能由这个点直接到达终点

       (1)如果能,那么冲上到终点的油量就可以了

       (2)如果不能,先看能否到下一站点,如果不能,GG,如果可以则转移到下一站点.


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 510;struct node{    double price, dist;    bool operator<(const node & X) const{        return dist < X.dist;    }}Node[N];int main() {    double c, d, avg;    int n;    scanf("%lf%lf%lf%d", &c, &d, &avg, &n);    for (int i = 0; i < n; ++i)         scanf("%lf%lf", &Node[i].price, &Node[i].dist);    sort(Node, Node + n);    double tank = 0.0, dist = 0.0, cost = 0.0;    if (Node[0].dist == 0) {        for (int i = 0; i < n;) {            int cheap_idx = i;            for (int j = i + 1; j < n && Node[j].dist - Node[i].dist <= avg * c; ++j) {                if (Node[j].price < Node[i].price) {                    cheap_idx = j;                    break;                }            }            if (cheap_idx > i) {                double needed = (Node[cheap_idx].dist - Node[i].dist) / avg;                if (tank > needed) {                    tank -= needed;                }                else {                    cost += (needed - tank) * Node[i].price;                    tank = 0.0;                }                i = cheap_idx;            }            else {                if (Node[i].dist + c * avg >= d) {                    cost += ((d - Node[i].dist) / avg - tank) * Node[i].price;                    dist = d;                    break;                }                else {                    cost += (c - tank) * Node[i].price;                    if (i < n - 1 && Node[i + 1].dist - Node[i].dist <= c * avg) {                        tank = c - (Node[i + 1].dist - Node[i].dist) / avg;                    }                    else {                        dist = Node[i].dist + c * avg;                        break;                    }                    ++i;                }            }        }    }    if (dist == d) printf("%.2lf\n", cost);    else printf("The maximum travel distance = %.2lf\n", dist);    return 0;}


0 0