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错误是如果出发点没有加油战的情况(特判)
2、两种情况:一、在汽车能到达的最大距离内,如果出现下一个加油站的价格比目前所在的加油站价格低,则只加刚好到达比目前便宜的加油站的油量。二、在汽车能到达的最大距离内,如果没出现比目前所在加油战油价低的加油站,则在目前加油站加满,到达(除目前加油站)下一个最大距离范围内的价格最低的加油站
3、为了计算最后一段路程我们可以把目的地设一个加油站价格为0,这样到达目的地前几站且能直接到达目的地时,可以直接按第一种情况直接到达目的地(方便)
我的代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 510;
struct Gas//加油站
{
double price,distance;
}gas[maxn];
struct Car//车
{
double cost,distance,nowstore,maxstore;
}car;
int cmax,d,davg,n,i = 0;
bool cmp(Gas g1,Gas g2)
{
return g1.distance < g2.distance;
}
int getmin(int i)//按情况得到最小价格的加油站序号
{
int k = i + 1,minid = i,temp = i + 1;
while (k <= n && car.maxstore * davg + gas[i].distance >= gas[k].distance)
{
if (gas[k].price < gas[minid].price)
{
minid = k;
break;
}
if (gas[k].price < gas[temp].price)
temp = k;
k++;
}
if (minid == i)
minid = temp;
return minid;
}
int main()
{
scanf("%d%d%d%d",&cmax,&d,&davg,&n);
car.maxstore = cmax;
for (int i = 0 ;i < n ;i++)
{
double price,distance;
scanf("%lf%lf",&price,&distance);
gas[i].distance = distance;
gas[i].price = price;
}
gas[n].distance = d;
gas[n].price = 0;
sort(gas,gas+n,cmp);
if (gas[0].distance != 0)
printf("The maximum travel distance = 0.00");
else
{
for (i = 0 ;i < n ;i++)
{
int minid = getmin(i);
if (car.maxstore * davg + gas[i].distance >= gas[minid].distance && gas[minid].price < gas[i].price)
{
car.cost += gas[i].price * ((gas[minid].distance - gas[i].distance) / davg - car.nowstore);
car.distance += gas[minid].distance - gas[i].distance;
car.nowstore = 0;
}
else
{
car.cost += (car.maxstore - car.nowstore) * gas[i].price;
if (gas[i+1].distance - gas[i].distance <= car.maxstore * davg)
car.distance += gas[minid].distance - gas[i].distance;
else
{
car.distance += car.maxstore * davg;
printf("The maximum travel distance = %.2f",car.distance);
break;
}
car.nowstore = car.maxstore - (gas[minid].distance - gas[i].distance) / davg;
}
i = minid - 1;
}
}
if (i == n)
printf("%.2f\n",car.cost);
return 0;
}