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 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: C m a x C_{max} Cmax(≤ 100), the maximum capacity of the tank; D D D (≤30000), the distance between Hangzhou and the destination city; D a v g D_ {avg} Davg(≤20), the average distance per unit gas that the car can run; and N N N(≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P i P_i Pi, the unit gas price, and D i D_i Di (≤ D D D), the distance between this station and Hangzhou, for i i i=1,⋯, N N 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
题目大意
从一个起点到终点的过程中有若干个加油点,每个加油点的油价不同。若一开始汽车的油箱为空,求解到终点时最少需要花的油费是多少。若无法到达终点,则输出最远能到哪里。
思路
- 使用结构体存放每个加油点所在的距离以及油价,并按照距离从近到远进行排序。
- 计算最少需要花的油费,就需要每一公里烧的油必须要是这一公里能加到的最便宜的油。
- 由于汽车邮箱最多存放 C m a x < = 100 C_{max}<=100 Cmax<=100单位的油,平均每单位的油能跑 D a v g < = 20 D_{avg}<=20 Davg<=20的距离,且一共有 N < = 500 N<=500 N<=500个加油点,可以看出题目中最远能到的距离为 100 ∗ 20 ∗ 500 = 1 0 6 100*20*500=10^6 100∗20∗500=106,因此开一个数组price[ 1 0 6 10^6 106],price[ i i i]用来存放从第 i − 1 i-1 i−1公里到第 i i i公里花能加到的最便宜的油。将price初始化为无穷大INF。
- 按照距离从近到远遍历所有的加油站,对每个加油点,从这个加油点所在的price[distance]开始往后遍历 C m a x ∗ D a v g C_{max}*D_{avg} Cmax∗Davg个距离(即加满一次油能跑的距离)。如果当前加油点的油价比遍历到的price[ j j j]要便宜,则更新price[ j j j]。
- 从1开始遍历price到终点 D D D,如果price[ i i i]等于无穷大,则能到最远的距离就是 i − 1 i-1 i−1。如果能到终点,则将所有公里的油价乘以每公里的耗油量,最终的到最少所需要花费的油费。
代码
#include <iostream>
#include <algorithm>
#include <vector>
#define MAXN 1000010
#define INF 99999999
using namespace std;
struct node{
double p;
int distance;
};
int cmax,d,davg,n;
double price[MAXN];
bool cmp(node a,node b){
return a.distance<b.distance;
}
int main(){
cin>>cmax>>d>>davg>>n;
vector<node> s(n);
fill(price,price+MAXN,INF);
for(int i=0;i<n;i++){
scanf("%lf %d",&s[i].p,&s[i].distance);
}
sort(s.begin(),s.end(),cmp);
int len=cmax*davg;
for(int i=0;i<n;i++){
for(int j=s[i].distance+1;j<=s[i].distance+len;j++){
if(s[i].p<price[j])price[j]=s[i].p;
}
}
double sum=0;
for(int i=1;i<=d;i++){
if(price[i]==INF){
printf("The maximum travel distance = %.2lf",(i-1)*1.0);
return 0;
}
sum+=price[i];
}
sum*=(1.0/davg);
printf("%.2lf",sum);
}