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 (Advanced Level) 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...

【C++】PAT (advanced level)1033. To Fill or Not to Fill (25)*

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

1033. To Fill or Not to Fill (25)-PAT甲级真题(贪心算法)

1033. To Fill or Not to Fill (25) With highways available, driving a car from Hangzhou to any other...
  • liuchuo
  • liuchuo
  • 2016年09月10日 21:26
  • 616

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

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

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

贪婪算法#include #include #define SIZE 501 #define INF 0x7fffffff using std::sort; struct gas{ doubl...

1033. To Fill or Not to Fill (25)【贪心+模拟】——PAT (Advanced Level) Practise

题目信息1033. To Fill or Not to Fill (25)时间限制10 ms 内存限制65536 kB 代码长度限制16000 BWith highways available, ...

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

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 ...

【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 1033. To Fill or Not to Fill (25)(贪心)

解题思路: 其实题目本身不难,难就难在对贪心策略的选取 在每一个加油点应该做这样的判断: 1、首先计算从本加油点做多可能达到的加油点 2、如果有可达加油点的话: 2.1 寻找有无比当前加油点油价便宜...

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

陷阱:没有距离为0加油站,则汽车无法行驶( ´◔ ‸◔’)#include #include using namespace std; typedef struct Sta { doubl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:PAT 1047 To Fill or Not to Fill (25)(高速公路加油问题)
举报原因:
原因补充:

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