PAT 1047 To Fill or Not to Fill (25)(高速公路加油问题)

题目

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00

解题思路

  • 1.解题报告
  • 2.总的来说是让邮箱装一直满油,然后油箱按价格分层,每次消耗的是最便宜的,到一个站后,如果油比他贵的油,就把所有贵的油换成这个站的油,然后再把加箱加满,这样肯定就保持了每次消耗的都是最便宜的油了。
  • 3.伪代码:
    1:用cost,now,oil来更新和保存当前消费,位置和剩余油量,并用一个deque 来保存油箱,记录好每层油箱的价格和存放的量。
    2:
    (1)每到一个站,计算需要走的距离d及需要的油量need。
    (2)如果油箱不为空且d还没走完,分层消耗油,直到油箱为空或者d走完为止,并跟新need,d,cost,now,iol等。
    (3)如果油走完了但是d还不为0,则退出。
    (4)如果d走完了,则将比这一站贵的油全部拍粗来,并在本站加满油箱。
  • 4.关键点是设置一个now,表示当前位置,方便理解以及后续计算,再者就是用一个东西来装分层油箱中每层的价格和装的油量。

代码

#include<iostream>
#include<vector>
#include<deque>
#include<algorithm>
#include<iomanip>
using namespace std;
struct station{
    double p,s;
    station ():p(0.0),s(0.0){}
    station (double _p,double _s){
        this->p = _p;
        this->s = _s;
    }
};
bool cmp(const station &a,const station &b){
    return a.s <b.s;
}
struct car{
    double p,v;
    car(){}
    car(double _p,double _v){
        this->p = _p;
        this->v = _v;
    }
};

int main(int argc, char *argv[])
{
    double Cmax,D,Davg;int N;
    cin >> Cmax >> D >> Davg >> N;
    vector<station> sta(N+1);
    for (int i = 0; i < N; ++i) {
        cin >> sta[i].p >> sta[i].s;
    }
    sta[N] = station(0,D);
    sort(sta.begin(),sta.end(),cmp);
    deque<car> a;
    double now = 0.,cost = 0.,oil = 0.;
    for (int i = 0; i < N + 1; ++i) {
        //1.计算到这一站需要走的距离d
        double d = sta[i].s - now;

        //2.如果油箱还有油并且d还没走完,分层消耗油,并更新消耗以及d
        while (!a.empty()&&d>0) {
            double need = d/Davg;
            //如果上面一层小于所需要的油量
            if (a.front().v<need) {
                now += a.front().v*Davg;
                cost += a.front().v*a.front().p;
                oil -= a.front().v;
                d -= a.front().v*Davg;
                a.pop_front();
            }
            else {
                now += d;
                d = 0;
                cost += need*a.front().p;
                a.front().v -= need;
                oil -= need;
            }
        }
        //3.如果油箱走完了,d>0,则退出,输出当前走的最远的距离
        if (d>0) {
            break;
        }

        //4.否则将比这一站贵的油全部拍粗来,在本站加满油箱
        while (!a.empty()&&(a.back().p>sta[i].p)) {
            oil -= a.back().v;
            a.pop_back();
        }
        if (oil < Cmax) {
            a.push_back(car(sta[i].p,Cmax - oil));
            oil = Cmax;
        }
    }
    if (now ==D) {
        cout << fixed << setprecision(2) << cost <<endl;
    }
    else {
        cout << "The maximum travel distance = " << fixed << setprecision(2) << now <<endl;
    }
    return 0;
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16949707/article/details/52351945
文章标签: PAT
个人分类: PAT
上一篇PAT 1050 Course List for Student (25)(选课)
下一篇PAT 1046 Sharing (25)(不同长度字符串交点)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭