问题 C: To Fill or Not to Fill
Posted ailinal
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了问题 C: To Fill or Not to Fill相关的知识,希望对你有一定的参考价值。
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
struct station {
double per_price;
double distance;
};
bool cmp(const station &s1, const station &s2) {
return s1.distance < s2.distance;
}
int main() {
double tank_c, distance, unit_d;
int n;
while (scanf(" %lf%lf%lf%d", &tank_c, &distance, &unit_d, &n) != EOF) {
double cost = 0;
double current_dis = 0;
double current_tank = 0;
double MAX_LEN = tank_c * unit_d;
station stations[n + 1];
for (int i = 0; i < n; ++i) {
scanf(" %lf %lf", &stations[i].per_price, &stations[i].distance);
}
station temp = {0, distance};
stations[n++] = temp;
sort(stations, stations + n, cmp);
if (stations[0].distance != 0) {
printf("The maximum travel distance = 0.00
");
break;
}
//当前站点
int k = 0;
while (current_dis < distance) {
int nextK = k;
int min_k = k;
double min_price = 999;
double max_distance = MAX_LEN + current_dis;
if (stations[k + 1].distance - current_dis > MAX_LEN) break;
bool flag = false;
for (int i = k + 1; i < n && stations[i].distance <= max_distance; ++i) {
if (stations[i].per_price < stations[k].per_price) {
flag = true;
nextK = i;
break;
}
if (stations[i].per_price < min_price) {
min_price = stations[i].per_price;
min_k = i;
}
}
double left = current_tank * unit_d;
//
if (flag) {
cost += ((stations[nextK].distance -stations[k].distance - left) / unit_d) * stations[k].per_price;
k = nextK;
current_dis = stations[nextK].distance;
current_tank = 0;
} else {
//加满
cost += ((MAX_LEN -left) / unit_d) * stations[k].per_price;
current_dis = stations[min_k].distance;
current_tank = tank_c - (stations[min_k].distance - stations[k].distance)/unit_d;
k = min_k;
}
}
if (current_dis == distance)
printf("%.2lf
", cost);
else
printf("The maximum travel distance = %.2lf
", stations[k].distance + MAX_LEN);
}
return 0;
}
以上是关于问题 C: To Fill or Not to Fill的主要内容,如果未能解决你的问题,请参考以下文章
PAT甲级1033 To Fill or Not to Fill (25 分)(贪心,思维可以做出简单解)