题解:B4196 [2023 海淀区小学组] 赛车游戏
首先预处理
钦定两个人最终在第
此时考虑让两个人中用时小的一个先走,如果他用这个时间差将
时间复杂度
#include <iostream>
#include <iomanip>
using namespace std;
const int kN = 1e5 + 5;
const double Inf = 1e9;
int n;
int a[kN];
double p[kN], s[kN];
void Solve () {
cin >> n >> a[n + 1];
for (int i = 1; i <= n; ++i)
cin >> a[i];
p[0] = 0;
for (int i = 1; i <= n; ++i)
p[i] = p[i - 1] + (a[i] - a[i - 1]) * 1.0 / i;
s[n + 1] = 0;
for (int i = n; i; --i)
s[i] = s[i + 1] + (a[i + 1] - a[i]) * 1.0 / (n - i + 1);
double ans = Inf;
for (int i = 0; i <= n; ++i) {
double d = a[i + 1] - a[i];
double x = p[i], y = s[i + 1];
if (x < y) {
d -= (y - x) * (i + 1);
x = y;
}
else {
d -= (x - y) * (n - i + 1);
y = x;
}
if (d > 0)
x += d / (n + 2);
ans = min(ans, x);
}
cout << fixed << setprecision(12) << ans << '\n';
}
int main () {
cin.tie(0)->sync_with_stdio(0);
int T;
cin >> T;
while (T--) Solve();
return 0;
}