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


• 本文已收录于以下专栏：

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

• dream_never_giveup
• 2016年12月25日 17:34
• 282

## PAT (Advanced Level) Practise 1033 To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 ...
• jtjy568805874
• 2016年03月09日 17:05
• 313

## 【PAT 1033】 To Fill or Not to Fill 贪心算法&模拟

1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 ...
• tiantangrenjian
• 2014年02月13日 22:49
• 3299

## solution Of Pat 1033. To Fill or Not to Fill (25)

1033. To Fill or Not to Fill (25)With highways available, driving a car from Hangzhou to any other c...
• qq_30490125
• 2016年03月29日 01:21
• 479

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

1033. To Fill or Not to Fill (25) 开始用dp写，只过了2个，真心蛋疼。网上看了是贪心，我的思路是： 当前在第i个位置，gas表示剩余汽油能走的距离 找到下个能到达的比...
• zhengweijian15
• 2014年02月25日 12:17
• 468

## PAT 1004 To Fill or Not to Fill (25)

• u011694809
• 2015年06月06日 09:09
• 671

## PAT 1033. To Fill or Not to Fill (25)（加油问题）(待修改)

• qq_16949707
• 2016年09月08日 10:20
• 89

## 【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...
• realxuejin
• 2013年10月31日 15:58
• 912

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

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c...
• zoushidexing
• 2013年08月29日 15:58
• 571

## 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...
• jolivan
• 2017年02月19日 01:23
• 91

举报原因： 您举报文章：PAT 1047 To Fill or Not to Fill (25)（高速公路加油问题） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)