1033 To Fill or Not to Fill

点点圈 提交于 2020-03-07 08:38:36

link

 

 

#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <climits>
#include <unordered_map>
#include <cstdio>
#include <iostream>

# define LL long long
using namespace std;

struct Station{
    double dis;
    double price;
};

int main(){
    double cmax, D, davg;
    int N;
    scanf("%lf %lf %lf %d", &cmax, &D, &davg, &N);
    vector<Station> stas(N+1);
    stas[0].dis=D;
    stas[0].price=0;
    for(int i=1;i<=N;i++){
        scanf("%lf %lf", &stas[i].price, &stas[i].dis);
    }
    sort(stas.begin(),stas.end(),[](Station s1, Station s2){
        return s1.dis<s2.dis;
    });

    if(stas[0].dis!=0){
        printf("The maximum travel distance = 0.00\n");
        return 0;
    }

    double curdis=0.0, curprice=stas[0].price, curgas=0.0, maxdis=0.0;
    double res=0.0;
    while(curdis<D){
        maxdis=curdis+cmax*davg;
        bool findLow=false;
        double minprice=99999999;
        double nextdis=0.0;
        for(int i=1;i<=N;i++){
            if(stas[i].dis<=curdis) continue;
            if(stas[i].dis>maxdis) break;
            if(stas[i].price<curprice){
                double gasNeeded=(stas[i].dis-curdis)/davg;
                res+=(gasNeeded-curgas)*curprice;

                findLow=true;
                curdis=stas[i].dis;
                curgas=0;
                curprice=stas[i].price;
                break;
            }
            if(stas[i].price<=minprice){
                minprice=stas[i].price;
                nextdis=stas[i].dis;
            }
        }
        if(findLow) continue;
        if(minprice==99999999){
            printf("The maximum travel distance = %.2f\n",maxdis);
            return 0;
        }
        res+=(cmax-curgas)*curprice;
        curgas=cmax-(nextdis-curdis)/davg;
        curdis=nextdis;
        curprice=minprice;
    }
    printf("%.2f\n",res);
    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!