【PAT 1033】To Fill or Not to Fill (struct、sort) 贪心算法

题目

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

代码长度限制 16 KB
时间限制 200 ms
内存限制 64 MB

题意

已知起点与终点的距离为D,油箱的最大油量为Cmax,单位汽油能够支持前进Davg。给定N个加油站的单位油价和离起点的距离(所有加油站都在一条线上),汽车初始时刻处于起点位置,油箱为空,且可以在任意加油站购买任意量的汽油(前提是不超过油箱容量),求从起点到终点的最小花费。如果无法到达终点,则输出能够行驶的最远距离。

简析

结合案例1进行贪心模拟

序号		单价		距离		花费						备注
 0	 	7.10    0                   
 1		7.00    150     88.75       
 2		7.20    200                 
 3		6.85    300     88.75+87.5=176.25       加满
 4		7.50    400                 
 5		7.00    600     176.25+342.5=518.75     加满
 6		7.30    1000    518.75+175=693.75         
 7		6.00    1250    693.75+30.42=724.17       
 8		0       1300    724.17+25=749.17          

①当前处于0号加油站,长度600内能到达1、2、3×4、5号加油站。在这些加油站中,能找到第一个油价低于当前油价的加油站,即1号(7.00<7.10),因此加恰好能够到达1号加油站的油,然后前往1号加油站。
②当前处于1号加油站,长度600内能到达2、3、4、5号加油站。在这些加油站中,能找到第一个油价低于当前油价的加油站,即3号(6.85<7.00),因此加恰好能够到达3号加油站的油,然后前往3号加油站。
③当前处于3号加油站,长度600内能到达4、5号加油站。在这些加油站中,找不到油价低于当前油价的加油站,因此选择其中油价最低的加油站,即5号。然后在当前加油站加满油(因为当前加油站的油价便宜),前往5号加油站。
④当前处于5号加油站,长度600内能到达6号加油站。在这些加油站中(虽然只有一个加油站能到达),找不到油价低于当前油价的加油站,因此选择其中油价最低的加油站,即6号。然后在当前加油站加满油,前往6号加油站。
⑤当前处于6号加油站,长度600内能到达7、8号加油站。在这些加油站中,能找到第一个油价低于当前油价的加油站,即7号(6.00<7.30),因此加恰好能够到7号加油站的油,然后前往7号加油站。
⑥当前处于7号加油站,长度600内能到达8号加油站。在这些加油站中,能找到第一个油价低于当前油价的加油站,即8号(0.00<6.00),因此加恰好能够到8号加油站的油,然后前往8号加油站。由于8号加油站即为终点,因此算法结束。

代码

#include <cstdio>
#include <algorithm>
using namespace std;

struct station {
    double price, dis;
}sta[510];

bool cmp(station a, station b)
{
    return a.dis < b.dis;    	//根据距离进行升序排序
}

int main()
{
    double cap, distance, D, MAX;		//都要设为浮点类型
    int N;
    scanf("%lf%lf%lf%d", &cap, &distance, &D, &N);
    MAX = cap * D;
    for(int i = 0; i < N; i++)
    {
        scanf("%lf%lf", &sta[i].price, &sta[i].dis);
    }
    //为终点赋值
    sta[N].price = 0;
    sta[N].dis = distance;
    sort(sta, sta + N, cmp);
    if(sta[0].dis != 0)			//在距离为0处必须有加油站,否则无法出发,因此无法到达终点
    {
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }
    //如果中间有任意一段距离超过最大续航,则无法到达终点,算出最远到达距离
    for(int i = 1; i <= N; i++)
    {
        int res = sta[i].dis - sta[i - 1].dis;
        if(res > MAX)
        {
            printf("The maximum travel distance = %.2f\n", sta[i - 1].dis + MAX);
            return 0;
        }
    }
    
    int k = 0;
    double ans = 0;
    double tank = 0;        //当前油量
    bool flag;
    
    while(k != N)
    {
        int min = k + 1;
        flag = false;
        for (int i = k + 1; sta[i].dis <= sta[k].dis + MAX ; i ++ )
        {
            if(sta[i].price < sta[k].price)
            {
                double need = (sta[i].dis - sta[k].dis) / D;	//需要油量
                if(need > tank)
                {
                    ans += (need - tank) * sta[k].price;
                    tank = 0;
                }
                else tank -= need;
                k = i;				//更新站台
                flag = true;
                break;
            }
            if(sta[i].price < sta[min].price)   min = i;
        }
        //如果没有比当前加油站价格还小的,则在当前站将油箱加满
        if(!flag)
        {
            ans += (cap - tank) * sta[k].price;
            //要减去到达下一站所需的油量
            tank = cap - (sta[min].dis - sta[k].dis) / D;	
            k = min;			//更新站台
        }
        //printf("%d  %.2f\n", k, ans);
    }
    printf("%.2f\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值