#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
struct gas_station
{
double price, dist;
}gs[501];
bool cmp(const gas_station &a, const gas_station &b)
{
return a.dist < b.dist;
}
int main()
{
double Cmax, D, Davg;
int N;
cin >> Cmax >> D >> Davg >> N;
for (int i = 0; i < N; i++)
{
cin >> gs[i].price >> gs[i].dist;
}
sort(gs, gs + N, cmp);
if (gs[0].dist != 0)
{
cout << "The maximum travel distance = 0.00" << endl;
return 0;
}
bool arrived = false;
int cur_index = 0;
double residue = 0, needgas = 0;
double total_cost = 0;
double max_run_dist = Cmax * Davg;
while (!arrived)
{
bool more_gs = false;
bool cheaper = false;
double cheapest_price = 10000;
int cheapest_index;
for (int i = cur_index + 1; i < N; i++)
{
if (gs[i].dist - gs[cur_index].dist <= max_run_dist)
{
more_gs = true;
if (gs[i].price < gs[cur_index].price)
{
cheaper = true;
needgas = (gs[i].dist - gs[cur_index].dist) / Davg - residue;
residue = 0;
total_cost += needgas * gs[cur_index].price;
cur_index = i;
break;
}
if (gs[i].price < cheapest_price)
{
cheapest_price = gs[i].price;
cheapest_index = i;
}
}
else
break;
}
if (!cheaper && D - gs[cur_index].dist <= max_run_dist)
{
needgas = (D - gs[cur_index].dist) / Davg - residue;
total_cost += needgas * gs[cur_index].price;
cout << fixed << setprecision(2) << total_cost << endl;
return 0;
}
if (!cheaper && more_gs)
{
needgas = Cmax - residue;
total_cost += needgas * gs[cur_index].price;
residue = Cmax - (gs[cheapest_index].dist - gs[cur_index].dist) / Davg;
cur_index = cheapest_index;
}
else if (!more_gs)
{
cout << "The maximum travel distance = " << fixed << setprecision(2) << gs[cur_index].dist + max_run_dist << endl;
return 0;
}
}
}
参考:
http://www.cnblogs.com/XBWer/p/3866486.html