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

原创 2016年08月29日 12:27:24

题目

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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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, G...

pat-a1033. To Fill or Not to Fill (25)

这个题就是贪心(还是看了看宝典,2333) 贪心策略:从起点开始,依次往后,找到一个价格比目前加油站单价更低的加油站就只需要买刚好能到该加油站的油就行了。如果在能走的范围内所有加油站的单价都高于目前...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

PAT 1033. To Fill or Not to Fill (25)

基本思想: (1) 对加油站按照价格进行排序; (2)对于每个加油站,判断应该加入的油量(即能够跑的距离进行计算),与现有路程里的进行合并运算。参考Road结构体的...

PAT A 1033. To Fill or Not to Fill (25)

题目 With highways available, driving a car from Hangzhou to any other city is easy.  But since the t...

PAT 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 St...

【PAT】1033. To Fill or Not to Fill (25)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c...

PAT 1004 To Fill or Not to Fill (25)

题目描述 With highways available, driving a car from Hangzhou to any other city is easy. But since the ...

PAT A1033. To Fill or Not to Fill (25)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c...

PAT A1033. To Fill or Not to Fill (25)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c...

PAT A 1033. To Fill or Not to Fill (25)

PAT未AC题
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)