题解
2024-08-05 13:41:31
发布于:浙江
16阅读
0回复
0点赞
#include <bits/stdc++.h>
int main() {
int n;
stdcin >> n;
stdvector<stdarray<int, 4>> rects(n);
for (auto &v : rects)
for (int &x : v)
stdcin >> x;
int xs, ys, xt, yt, ex = 0;
double v;
std::cin >> xs >> ys >> xt >> yt >> v;
if (xs == xt)
return printf("%.8lf\n", std::abs(ys - yt) / v), 0;
if (xs > xt)
std::swap(xs, xt), std::swap(ys, yt);
std::vector<int> x, y, tp;
auto add = [&](int _x, int _y, int _tp) { x.emplace_back(_x), y.emplace_back(_y), tp.emplace_back(_tp); };
add(xs, ys, 0);
for (int i = 1; i < n; i++) {
if (rects[i][0] <= xs) {
if (rects[i][0] == xs) {
if (ys > rects[i][3])
ex += ys - rects[i][3], y[0] = rects[i][3];
if (ys < rects[i][1])
ex += rects[i][1] - ys, y[0] = rects[i][1];
}
continue;
}
if (rects[i][0] >= xt) {
if (rects[i][0] == xt) {
if (yt > rects[i - 1][3])
ex += yt - rects[i - 1][3], yt = rects[i - 1][3];
if (yt < rects[i - 1][1])
ex += rects[i - 1][1] - yt, yt = rects[i - 1][1];
}
break;
}
add(rects[i][0], std::min(rects[i - 1][3], rects[i][3]), 1);
add(rects[i][0], std::max(rects[i - 1][1], rects[i][1]), 2);
}
add(xt, yt, 1);
int m = x.size();
std::vector<double> f(m, 1e9);
f[0] = 0;
for (int i = 1; i < m; i++) {
double lower = -1e9, upper = 1e9;
for (int j = i - tp[i]; j >= 0; j--) {
double k = (double)(y[i] - y[j]) / (x[i] - x[j]);
if (lower <= k && k <= upper)
f[i] = std::min(f[i], f[j] + hypot(x[i] - x[j], y[i] - y[j]));
if (tp[j] == 1 && k > lower)
lower = k;
if (tp[j] == 2 && k < upper)
upper = k;
if (lower > upper)
break;
}
}
printf("%.8lf\n", (f.back() + ex) / v);
return 0;
}
这里空空如也
有帮助,赞一个