题目链接
- 把终点视为单位油价为0,里起点距离为D的加油站,起哨兵作用
- 假设当前所处加油站编号为now, 接下来从满油状态下能到达的所有加油站中选出下一个前往的加油站:
- 可以找到油价比当前加油站低的,加恰好能到达该加油站的油
- 找不到比当前低的,去最低的。先在当前加油站加满
- 找不到加油站,则最远能到达的距离即为当前加油站距离,加上满油到达的最远距离
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 500 + 5;
const int INF = (1 << 30) - 1;
struct station {
double price;
double dis;
} sta[maxn];
inline bool cmp(station a, station b)
{
return a.dis <= b.dis;
}
int main()
{
double cap, dis, davg;
int n;
scanf("%lf%lf%lf%d", &cap, &dis, &davg, &n);
for (int i = 0; i < n; ++i) {
scanf("%lf%lf", &sta[i].price, &sta[i].dis);
}
sort(sta, sta + n, cmp);
sta[n].price = 0, sta[n].dis = dis;
if (sta[0].dis != 0) {
printf("The maximum travel distance = 0.00");
}
else
{
double total = 0, maxdis = cap * davg, nowtank = 0;
int now = 0;
while (now < n) {
int p = -1;
double minprice = INF;
for (int i = now + 1; i <= n && sta[i].dis - sta[now].dis <= maxdis; ++i) {
if (sta[i].price < minprice) {
minprice = sta[i].price;
p = i;
}
if (minprice < sta[now].price)
break;
}
if (p == -1) break;
double need = (sta[p].dis - sta[now].dis) / davg;
if (minprice < sta[now].price) {
if (nowtank < need) {
total += (need - nowtank) * sta[now].price;
nowtank = 0;
}
else {
nowtank -= need;
}
}
else {
total += (cap - nowtank) * sta[now].price;
nowtank = cap - need;
}
now = p;
}
if (now == n) {
printf("%.2lf", total);
}
else {
printf("The maximum travel distance = %.2lf", sta[now].dis + maxdis);
}
}
return 0;
}