旅行家的预算
考虑贪心。
- 贪心地选择能到且油价更低的加油站
- 若不能直达油价更低的加油站,就加满(加满是为了下一站更贵的加油站加的油少些)油选择一个范围内能到的加油站
- 如果任意一个加油站加满油不能到下一个加油站那么答案不存在
贪心,模拟就可以。
#include <bits/stdc++.h>
#define x first
#define y second
typedef std::pair<double, double> PDD;
const int N = 550;
PDD a[N];
bool cmp(PDD a, PDD b) {
return a.x < b.x;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
double t, c, d, p1;
int n;
std::cin >> t >> c >> d >> p1 >> n;
a[0].y = p1, a[n + 1].x = t;
for (int i = 1; i <= n; i++) {
std::cin >> a[i].x >> a[i].y;
if (a[i].x - a[i - 1].x > c * d) {
std::cout << "No Solution\n";
return 0;
}
}
std::sort(a + 1, a + 1 + n, cmp);
int i = 0;
double ans = 0, cur = 0;
while (i <= n + 1) {
int j = i + 1, k = i + 1;
while (j <= n) {
if (a[j].y <= a[k].y) {
k = j;
}
if (a[i].y >= a[j].y) {
break;
}
if (a[j + 1].x - a[i].x > c * d) {
break;
}
j++;
}
if (a[j].y > a[i].y) {
ans += (c - cur) * a[i].y;
cur = c - (a[k].x - a[i].x) / d;
i = k;
} else {
ans += ((a[j].x - a[i].x) / d - cur) * a[i].y;
i = j;
cur = 0;
}
}
std::cout << std::fixed << std::setprecision(2) << ans << "\n";
return 0;
}