码迷,mamicode.com
首页 > 其他好文 > 详细

1033 To Fill or Not to Fill

时间:2020-03-07 09:43:40      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:sort   for   algo   begin   define   iostream   break   stat   asn   

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;
}

 

1033 To Fill or Not to Fill

标签:sort   for   algo   begin   define   iostream   break   stat   asn   

原文地址:https://www.cnblogs.com/FEIIEF/p/12432451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!