#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <limits>
#include <cfloat>
#include <cmath>
#define INF 1000000
using namespace std;
struct station {
double price, dist;
};
bool comp(station s1, station s2);
int main(void) {
int n, i, j;
double cmax, d, davg;
vector<station> gas_station;
station tmp;
scanf("%lf %lf %lf %d", &cmax, &d, &davg, &n);
for (i = 0; i < n; i++) {
scanf("%lf %lf", &tmp.price, &tmp.dist);
gas_station.push_back(tmp);
}
sort(gas_station.begin(), gas_station.end(), comp);
double totalprice = 0.0, remain = 0, present = 0;
i = 0;
int index;
double min_price;
if (fabs(gas_station[0].dist - present) > FLT_EPSILON) {
printf("The maximum travel distance = %.2lf\n", present);
return 0;
}
while (true) {
min_price = INF;
index = i + 1;
for (j = i + 1; j != n && gas_station[j].dist - present <= cmax * davg; j++) {
if (gas_station[i].price >= gas_station[j].price) {
min_price = gas_station[j].price;
index = j;
break;
}
}
if (min_price > gas_station[i].price) {
for (j = i + 1; j != n && gas_station[j].dist - present <= cmax * davg; j++) {
if (min_price > gas_station[j].price) {
min_price = gas_station[j].price;
index = j;
}
}
}
if ((index != n && gas_station[index].dist - present > cmax * davg) || (index == n && d - present > cmax * davg)) {
printf("The maximum travel distance = %.2lf\n", present + cmax * davg);
break;
}
if (min_price > gas_station[i].price) {
if (cmax * davg >= d - present) {
totalprice += (d - present) / davg * gas_station[i].price;
printf("%.2lf\n", totalprice);
break;
}
totalprice += (cmax - remain) * gas_station[i].price;
remain = cmax;
remain -= (gas_station[index].dist - present) / davg;
}
else {
if ((gas_station[index].dist - present) / davg - remain > 0) {
totalprice += ((gas_station[index].dist - present) / davg - remain) * gas_station[i].price;
remain = 0;
}
else {
remain -= (gas_station[index].dist - present) / davg;
}
}
present = gas_station[index].dist;
i = index;
}
return 0;
}
bool comp(station s1, station s2) {
return s1.dist < s2.dist;
}
1033. To Fill or Not to Fill (25)解题报告
最新推荐文章于 2022-08-13 11:37:07 发布