• PAT 1033. To Fill or Not to Fill


    #include <cstdio>
    #include <cstdlib>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        int N;
        double mcap, dist, davg; 
    
        scanf("%lf%lf%lf%d", &mcap, &dist, &davg, &N);
        double price, idist;
        
        vector<pair<double, double> > stations(N + 1);
        for (int i=0; i<N; i++) {
            scanf("%lf%lf", &price, &idist);
            stations[i].first = idist;
            stations[i].second= price;
        }
        
        // virtual station to indicate the end
        stations[N].first = dist;
        stations[N].second= 1e300;
        
        sort(stations.begin(), stations.end());
        
        double tank = 0, cur_dist = 0, cur_price = 0;
        
        if (stations[0].first > 0) {
            printf("The maximum travel distance = %.2lf", cur_dist);
            return 0;
        }
        
        double money     = 0;    
        int cur_stat    = 0;
        int cheap_stat     = -1;
        double cheap_price = 0;
        
        while (cur_dist < dist && (cur_stat) < N) {
            double next_dist = stations[cur_stat + 1].first;
            double max_dist  = mcap * davg + cur_dist;
            // can't reach next station
            if (max_dist < next_dist) {
                cur_dist = max_dist;
                break;
            }
            // can reach next station
            // find first min gas price from cur_stat/cheap_stat within the reach range
            if (cheap_stat < cur_stat) {
                // last cheap station we calculated has past by
                cheap_stat = cur_stat;
                cheap_price= stations[cur_stat].second;
            }
            for (int i=cheap_stat; i<N && stations[i].first <= max_dist; i++) {
                if (stations[i].second < cheap_price) {
                    cheap_stat = i;
                    cheap_price= stations[i].second;
                    break;
                }
            }
            // cheaper station is cur_stat
            if (cheap_stat == cur_stat) {
                // from here we can reach end station
                if (max_dist >= dist) {
                    money += cheap_price * (dist - cur_dist) / davg;
                    cur_dist = dist;
                    break;
                }
                // can't reach the end station
                // we should fill full tank
                money += (mcap - tank) * cheap_price;
                tank = mcap;
            } else {
                // cur_stat is not the cheaper station, 
                // just fill enough gas, go to the cheaper one
                double gas_need = (stations[cheap_stat].first - cur_dist) / davg;
                if (gas_need > tank) {
                    // we have to fill gas here
                    money += (gas_need - tank) * stations[cur_stat].second;
                    tank = gas_need;
                } else {
                    // we have the enough gas already
                }
                
            }
            // now we have enough gas
            // drive to the next station
            tank -= (next_dist - cur_dist) / davg;
            cur_stat++;
            cur_dist = next_dist;
        }
        if (cur_dist < dist) {
            printf("The maximum travel distance = %.2lf", cur_dist);
        } else {
            printf("%.2lf", money);
        }
        return 0;
    }

    写得有点烦

  • 相关阅读:
    Jenkins Pipeline Script from SCM应用
    Jenkins获取所有job
    Jenkins pipeline使用git共享库(ShareLibrary)
    Jenkins获取用户所属组
    PowerShell函数当做变量传递给另一个函数
    Jenkins input获取提交人
    Groovy Map排序
    Jenkins Pipeline使用File parameter
    Jenkins Active Parameters之Groovy Script(获取具有管理员权限的组+组成员)
    Android中C/C++的日志打印
  • 原文地址:https://www.cnblogs.com/lailailai/p/4116279.html
Copyright © 2020-2023  润新知