ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[PAT] A1033 To Fill or Not to Fill (25分)

2020-06-06 21:52:57  阅读:269  来源: 互联网

标签:25 begin 加满 PAT sta nowgas double 加油站 Fill


(重要!)
贪心

思路

首先将各加油站按照距离进行排序,如果没有距离为0的加油站,则直接输出The maximum travel distance = 0.00;若有则进行下面的判断。
对于每个加油站加多少最为合适?用贪心策略。
设当前我们站在加油站A。
1) 如果在A能到达的最大范围内(即假设在A加满油),有加油站的油价比当前站A的油价低:设距离A最近的且比A小的加油站为B,则加到能恰好到B加油站距离的油量就是一个局部最优策略。
2) 如果在最大的范围内没有加油站油价比当前低:则在A加满(加满是因为,无论在范围之外有多小多小的油价,即使加满也无法到达,还需要中间一个加油站中转,但既然当前油价最低,能加满就加满,之后油钱高的就可以少加点)。
不断的循环这个过程,在正常情况下到达目的地就终止循环,根据上述策略,可以将目的地也看做加油站,其距离设置为相应距离,油钱为0,这样其油钱一定比所有的加油站油钱都要低,在最后一定会选择目的地作为下一到达加油站。但是要注意,有可能当前加油站最近的一个加油站超过了车能行驶的最大距离(即最大的油量*每升行驶路程),则无法到达目的地,最长距离就是当前加油站的位置+车能行驶的最大距离。

tips

先判断如果第一个加油站不在起点,则直接输出无法到达。因为循环中的每次判断都是站在一个加油站的立场上的,如果0处没有加油站,循环内检查不出,所以要提前单独判断。

AC代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 502
struct station {
    double begin;
    double price;
};
double cmax;//油箱最大内存
double dissum;//总路程
double davg;//每单位油可以跑的公里数
int n;
vector<station>sta;
bool cmp(station a, station b) {
    return a.begin < b.begin;
}
int main() {
    scanf("%lf%lf%lf%d", &cmax, &dissum, &davg, &n);
    int i, j;
    station tempsta;
    for (i = 0;i < n;i++) {
        scanf("%lf%lf", &tempsta.price, &tempsta.begin);
        sta.push_back(tempsta);
    }
    tempsta.price = 0;
    tempsta.begin = dissum;
    sta.push_back(tempsta);
    sort(sta.begin(), sta.end(), cmp);

    double priceans = 0;
    double nowgas = 0;
    double fulldis = cmax * davg;//加满油可以走的公里数
    if (sta[0].begin != 0) {
        printf("The maximum travel distance = 0.00");
        return 0;
    }
    for (i = 0;i < n;i++) {
        j = i + 1;
        int flag = 0;
        double iffulldis = sta[i].begin + fulldis;
        while (sta[j].begin < iffulldis) {
            if (sta[j].price < sta[i].price) {
                flag = 1;
                break;
            }
            else j++;
        }
        if (flag == 0) {//在i处加满油
            priceans += (cmax - nowgas) * sta[i].price;
            nowgas = cmax;
        }
        else {//在i处只加到够跑到j
            if (nowgas < (sta[j].begin-sta[i].begin)/davg) {//i没加之前不能跑到j
                double need = (sta[j].begin - sta[i].begin) / davg - nowgas;
                priceans += need * sta[i].price;
                nowgas = nowgas + need;
            }
        }
        nowgas -= (sta[i + 1].begin - sta[i].begin) / davg;
        if (nowgas < 0) {
            double maxdis = sta[i].begin + fulldis;
            printf("The maximum travel distance = %.2lf", maxdis);
            return 0;
        }
    }
    printf("%.2lf", priceans);
    return 0;
}

标签:25,begin,加满,PAT,sta,nowgas,double,加油站,Fill
来源: https://www.cnblogs.com/yue36/p/13056640.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有