提交时间:2026-01-18 20:57:54

运行 ID: 83006

#include <iostream> #include <vector> #include <algorithm> #include <iomanip> #include <cmath> using namespace std; // 定义加油站结构体 struct Station { double distance; // 离出发点的距离 double price; // 油价 // 重载排序规则,按距离升序排列 bool operator<(const Station& other) const { return distance < other.distance; } }; int main() { double D1, C, D2, P_start; int N; // 读取基础参数:总距离、油箱容量、每升行驶距离、出发点油价、油站数 cin >> D1 >> C >> D2 >> P_start >> N; vector<Station> stations; // 添加出发点 stations.push_back({0.0, P_start}); // 添加沿途油站 for (int i = 0; i < N; ++i) { double di, pi; cin >> di >> pi; stations.push_back({di, pi}); } // 添加终点(虚拟油站,油价为0) stations.push_back({D1, 0.0}); // 按距离排序 sort(stations.begin(), stations.end()); // 检查输入合法性:距离是否递增、是否超过总距离 bool valid = true; for (int i = 1; i < stations.size(); ++i) { if (stations[i].distance > D1 || stations[i].distance <= stations[i-1].distance) { valid = false; break; } } if (!valid) { cout << "No Solution" << endl; return 0; } int current_station = 0; // 当前所在油站索引 double current_oil = 0.0; // 当前油箱油量 double total_cost = 0.0; // 总费用 double max_range = C * D2; // 油箱加满能行驶的最大距离 // 循环直到到达终点 while (current_station < stations.size() - 1) { double curr_dist = stations[current_station].distance; double curr_price = stations[current_station].price; // 检查是否能到达下一个油站(基础可达性) double next_dist = stations[current_station + 1].distance; if (next_dist - curr_dist > max_range) { cout << "No Solution" << endl; return 0; } // 在可达范围内找最便宜的油站 int min_price_idx = current_station; double min_price = curr_price; // 遍历所有可达的油站 for (int i = current_station + 1; i < stations.size(); ++i) { double dist = stations[i].distance; // 超出最大续航,停止遍历 if (dist - curr_dist > max_range) { break; } // 找到更便宜的油站,直接记录(贪心:优先去更便宜的) if (stations[i].price < min_price) { min_price = stations[i].price; min_price_idx = i; break; // 找到第一个更便宜的即可 } } if (min_price_idx == current_station) { // 当前油站是范围内最便宜的,加满油,开到下一个油站 double need_oil = C - current_oil; total_cost += need_oil * curr_price; current_oil = C; // 行驶到下一个油站 int next_station = current_station + 1; double drive_dist = stations[next_station].distance - curr_dist; current_oil -= drive_dist / D2; current_station = next_station; } else { // 有更便宜的油站,加刚好能到该油站的油量 double target_dist = stations[min_price_idx].distance; double need_oil_total = (target_dist - curr_dist) / D2; double need_oil = need_oil_total - current_oil; if (need_oil > 0) { total_cost += need_oil * curr_price; current_oil = need_oil_total; } // 行驶到目标油站 double drive_dist = target_dist - curr_dist; current_oil -= drive_dist / D2; current_station = min_price_idx; } } // 输出结果,四舍五入到两位小数 cout << fixed << setprecision(2) << round(total_cost * 100) / 100 << endl; return 0; }