#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; a <= i; i--) #define cat(x) cerr << #x << " = " << x << '\n'; using ll = long long; using ld = long double; using namespace std; const int N = 1 << 18; const ld eps = 1e-12; struct Tree { ll t[2 * N]; void add(int x, ll y, int u = 1, int l = 0, int r = N - 1) { if (l == r) { t[u] = y; return; } int m = (l + r) / 2; if (x <= m) add(x, y, 2 * u, l, m); else add(x, y, 2 * u + 1, m + 1, r); t[u] = max(t[2 * u], t[2 * u + 1]); } int query(int x, ll need, int u = 1, int l = 0, int r = N - 1) { if (r < x || t[u] < need) return 0; if (l == r) return l; int m = (l + r) / 2; int L = query(x, need, 2 * u, l, m); if (L) return L; return query(x, need, 2 * u + 1, m + 1, r); } } Tr; int n, pointer[N]; string s1, s2, s3; ld T, P, v0, v1, v2, v3; vector<ld> w1, w3; /* 1....2 -> ....12 */ ld f(ld p1, ld v1, ld p2, ld v2) { return (p2 - p1 - 1) / (v1 - v2); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> v0 >> v1 >> v2 >> v3; cin >> s1 >> s2 >> s3; rep(i, 0, n - 1) if (s1[i] == '#') { assert(i > 0); w1.push_back(i); if (s1[i - 1] == '#') pointer[i] = pointer[i - 1]; else pointer[i] = i; } w3.push_back(1e15); per(i, 1, n - 1) if (s3[i] == '#') { Tr.add(i, w3.back() - i - 1); w3.push_back(i); } reverse(w3.begin(), w3.end()); int a = 0; while (a < n) { int l = a; while (l < n && s2[l] == '.') l++; if (l == n) break; int r = l; while (r < n && s2[r] == '#') r++; r--; ld x = f(P, v0, l + v2 * T, v2); T += x; P += x * v0; ld opt_time = f(r + v2 * T, v2, P, v0); auto up = [&]() { auto it = lower_bound(w1.begin(), w1.end(), P - 1 + eps - v1 * T); if (it == w1.end()) return opt_time; int id = pointer[int(*it)]; return max(opt_time, f(r + v2 * T, v2, id - 1 + v1 * T, v1)); }; auto down = [&]() { auto it = lower_bound(w3.begin(), w3.end(), P - 1 + eps - v3 * T); if (it == w3.end()) return opt_time; ld min_distance = opt_time * (v0 - v3) + 1; if ((*it + T * v3) - P >= min_distance - eps) { return opt_time; } int id = Tr.query(int(*it), ceil(min_distance - eps)); return f(id + T * v3, v3, P, v2) + opt_time; }; ld opt = min(up(), down()); T += opt; P = r + T * v2 + 1; a = r + 1; } rep(i, 1, n - 1) { if (s1[i] == '#') { ld p1 = i + T * v1; if (p1 >= P - 1 - eps) { ld x = f(p1, v1, P, v0); T += x; P += v0 * x; } } if (s3[i] == '#') { ld p3 = i + T * v3; if (p3 >= P - 1 - eps) { ld x = f(p3, v3, P, v0); T += x; P += v0 * x; } } } cout << fixed << setprecision(15) << T << '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; a <= i; i--) #define cat(x) cerr << #x << " = " << x << '\n'; using ll = long long; using ld = long double; using namespace std; const int N = 1 << 18; const ld eps = 1e-12; struct Tree { ll t[2 * N]; void add(int x, ll y, int u = 1, int l = 0, int r = N - 1) { if (l == r) { t[u] = y; return; } int m = (l + r) / 2; if (x <= m) add(x, y, 2 * u, l, m); else add(x, y, 2 * u + 1, m + 1, r); t[u] = max(t[2 * u], t[2 * u + 1]); } int query(int x, ll need, int u = 1, int l = 0, int r = N - 1) { if (r < x || t[u] < need) return 0; if (l == r) return l; int m = (l + r) / 2; int L = query(x, need, 2 * u, l, m); if (L) return L; return query(x, need, 2 * u + 1, m + 1, r); } } Tr; int n, pointer[N]; string s1, s2, s3; ld T, P, v0, v1, v2, v3; vector<ld> w1, w3; /* 1....2 -> ....12 */ ld f(ld p1, ld v1, ld p2, ld v2) { return (p2 - p1 - 1) / (v1 - v2); } int main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> v0 >> v1 >> v2 >> v3; cin >> s1 >> s2 >> s3; rep(i, 0, n - 1) if (s1[i] == '#') { assert(i > 0); w1.push_back(i); if (s1[i - 1] == '#') pointer[i] = pointer[i - 1]; else pointer[i] = i; } w3.push_back(1e15); per(i, 1, n - 1) if (s3[i] == '#') { Tr.add(i, w3.back() - i - 1); w3.push_back(i); } reverse(w3.begin(), w3.end()); int a = 0; while (a < n) { int l = a; while (l < n && s2[l] == '.') l++; if (l == n) break; int r = l; while (r < n && s2[r] == '#') r++; r--; ld x = f(P, v0, l + v2 * T, v2); T += x; P += x * v0; ld opt_time = f(r + v2 * T, v2, P, v0); auto up = [&]() { auto it = lower_bound(w1.begin(), w1.end(), P - 1 + eps - v1 * T); if (it == w1.end()) return opt_time; int id = pointer[int(*it)]; return max(opt_time, f(r + v2 * T, v2, id - 1 + v1 * T, v1)); }; auto down = [&]() { auto it = lower_bound(w3.begin(), w3.end(), P - 1 + eps - v3 * T); if (it == w3.end()) return opt_time; ld min_distance = opt_time * (v0 - v3) + 1; if ((*it + T * v3) - P >= min_distance - eps) { return opt_time; } int id = Tr.query(int(*it), ceil(min_distance - eps)); return f(id + T * v3, v3, P, v2) + opt_time; }; ld opt = min(up(), down()); T += opt; P = r + T * v2 + 1; a = r + 1; } rep(i, 1, n - 1) { if (s1[i] == '#') { ld p1 = i + T * v1; if (p1 >= P - 1 - eps) { ld x = f(p1, v1, P, v0); T += x; P += v0 * x; } } if (s3[i] == '#') { ld p3 = i + T * v3; if (p3 >= P - 1 - eps) { ld x = f(p3, v3, P, v0); T += x; P += v0 * x; } } } cout << fixed << setprecision(15) << T << '\n'; return 0; } |