真复杂。。。
#include<iostream>//AC//建议看算法笔记
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 550;
double gasCap, dsum, disPerGas;
int n;
struct node {
double price, d;
};
bool cmp(node a, node b) { return a.d < b.d; }
vector<node> sta;
int main() {
cin >> gasCap >> dsum >> disPerGas >> n;
double maxLen = gasCap * disPerGas;
for (int i = 0; i < n; i++) {
double a, b;
cin >> a >> b;
sta.push_back({ a,b });
}
sta.push_back({ 1e9,dsum });
n++;
sort(sta.begin(), sta.end(), cmp);
if (sta[0].d > 0) {
printf("The maximum travel distance = 0.00");
return 0;
}
int p = 0;
double ans = 0, leftGas = 0;
while (p < n - 1) {
int next = p + 1;
if (maxLen < (sta[next].d - sta[p].d)) {
double maxD = sta[p].d + maxLen;
printf("The maximum travel distance = %.2f", maxD);
return 0;
}
double minPrice = sta[p].price, minSta = -1;
while (next < n && sta[next].d - sta[p].d <= maxLen) {
if (sta[next].price <= minPrice) {
minPrice = sta[next].price;
minSta = next;
break;///到比自己低的(注意不是选最低,而是选比自己低的里面离自己最近的那个)
}
next++;
}
if (minSta == -1) {//最大量
double l = min((dsum - sta[p].d), maxLen);
for (next = p; next < n; next++) {
if (l + sta[p].d < sta[next].d) { break; }
}
next--;
ans += ((l / disPerGas - leftGas) * sta[p].price);
leftGas = gasCap - ((sta[next].d - sta[p].d) / disPerGas);
}
else {//中途停
next = minSta;
double l = sta[next].d - sta[p].d;
ans += (((l / disPerGas) - leftGas) * sta[p].price);
leftGas = 0;
}
p = next;
}
printf("%.2f", ans);
return 0;
}