zoukankan      html  css  js  c++  java
  • 1033 To Fill or Not to Fill

    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
    ");
            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
    ",maxdis);
                return 0;
            }
            res+=(cmax-curgas)*curprice;
            curgas=cmax-(nextdis-curdis)/davg;
            curdis=nextdis;
            curprice=minprice;
        }
        printf("%.2f
    ",res);
        return 0;
    }
  • 相关阅读:
    C/C++位域(Bitfields)之我见(不错)
    C/C++位域(Bitfields)之我见(不错)
    C语言面试题之一——程序阅读题
    Matlab数据导入导出
    C语言大小端判断程序
    Qt——路径设置问题
    C语言大小端判断程序
    Qt——路径设置问题
    poj3625
    poj3660
  • 原文地址:https://www.cnblogs.com/FEIIEF/p/12432451.html
Copyright © 2011-2022 走看看