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

PAT 1033. To Fill or Not to Fill

时间:2014-11-23 12:55:37      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   on   div   log   

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

写得有点烦

PAT 1033. To Fill or Not to Fill

标签:style   blog   io   color   sp   for   on   div   log   

原文地址:http://www.cnblogs.com/lailailai/p/4116279.html

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