#include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < int(n); ++i) #define FO(i, a, b) for (int i = (a); i < int(b); ++i) #define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((b) < (a) ? (a) : (b)) #define REMIN(a, b) ((a) = min(a, b)) #define REMAX(a, b) ((a) = max(a, b)) #define ALL(c) (c).begin(), (c).end() #define SQR(x) ((x) * (x)) #define PRINT(x) cerr << #x << " == " << x << endl; #define ZERO(x) memset(x, 0, sizeof(x)); #define endl '\n' using LD = long double; #define ASS(x) \ if (!(x)) \ cerr << "ASSERT FAILED " #x << " " << __FILE__ << ':' << __LINE__ << endl; // const bool READ_NUM_TEST_CASES = false; const int N = 200'000 + 9; const int NN = 1024 * 256; struct Tree { int max_hole = 0; int max_pref = 0; int max_suff = 0; }; Tree tree[2 * NN]; void tree_set(int x, bool last = false) { x += NN; tree[x].max_hole = tree[x].max_pref = tree[x].max_suff = last ? INT_MAX / 2 : 1; int width = 1; while (x) { x /= 2; tree[x].max_hole = max({tree[x * 2].max_hole, tree[x * 2 + 1].max_hole, tree[x * 2].max_suff + tree[x * 2 + 1].max_pref}); tree[x].max_suff = tree[x * 2 + 1].max_hole == width ? tree[x * 2].max_suff + width : tree[x * 2 + 1].max_suff; tree[x].max_pref = tree[x * 2].max_hole == width ? tree[x * 2 + 1].max_pref + width : tree[x * 2].max_pref; width *= 2; } } int tree_find(int x0, int len, int x, int a, int b) { // cerr << "tree_find" << endl; // PRINT(x0) // PRINT(len) // PRINT(x) // PRINT(a) // PRINT(b) // cerr << endl; // this_thread::sleep_for(100ms); int mid = (a + b) / 2; if (a + 1 == b) { if (tree[x].max_hole >= len) return a; else return INT_MAX; } if (x0 >= mid) return tree_find(x0, len, x * 2 + 1, mid, b); if (tree[x * 2].max_hole >= len) { auto r = tree_find(x0, len, x * 2, a, mid); if (r != INT_MAX) return r; } REMAX(x0, mid - tree[x * 2].max_suff); if ((mid - x0) + tree[x * 2 + 1].max_pref >= len) return x0; return tree_find(x0, len, x * 2 + 1, mid, b); } struct TC { int L; int vs[3]; int v0; char lanes[3][N]; LD res[N]; int prev_free[N]; int ride_until[N]; int hole_fr[N]; void solve() { cin >> L >> v0; FOR(i, 3) cin >> vs[i]; FOR(i, 3) { string s; cin >> s; FOR(j, L) lanes[i][j] = (s[j] == '#'); lanes[i][L] = 0; } lanes[2][0] = 0; FOR(j, L) { if (lanes[0][j]) prev_free[j] = prev_free[j - 1]; else prev_free[j] = j; } ride_until[L] = L; OF(j, 0, L) { if (lanes[0][j + 1]) ride_until[j] = j; else ride_until[j] = ride_until[j + 1]; } hole_fr[L] = INT_MAX / 2; OF(j, 0, L) { if (lanes[2][j]) hole_fr[j] = 0; else hole_fr[j] = hole_fr[j + 1] + 1; } FOR(j, L) if (!lanes[2][j]) tree_set(j); tree_set(L, true); LD time = 0; int x = 0; int last = -1; FOR(j, L) if (lanes[1][j]) last = j; while (x < last + 1) { int next_x = x; while (lanes[1][next_x + 1] == 0 && next_x < last + 1) ++next_x; if (next_x != x) { time += LD(next_x - x) / (v0 - vs[1]); x = next_x; continue; } next_x += 1; while (lanes[1][next_x]) ++next_x; // cerr << endl // << "time " << time << " jump " << x << " -> " << next_x << endl; const LD EPS = 1e-12; // up LD up_time = time; LD up_time_diff = 0; LD up_x = x - time * (vs[0] - vs[1]); if (lanes[0][max(0, int(up_x + EPS))] || lanes[0][max(0, int(up_x + 1 - EPS))]) { up_time_diff = (up_x - prev_free[int(up_x + EPS)]) / (vs[0] - vs[1]); up_time += up_time_diff; up_x = x - up_time * (vs[0] - vs[1]); } LD collide_time = (ride_until[int(up_x + EPS)] - up_x) / (v0 - vs[0]); LD base_reach_time = LD(next_x - x) / (v0 - vs[1]); // PRINT(collide_time); // PRINT(base_reach_time); LD reach_time = base_reach_time + up_time_diff; // must wait in top lane if (reach_time > collide_time) { // LD collide_x = x + collide_time * (v0 - vs[1]); reach_time = collide_time + (next_x - x - collide_time * (v0 - vs[1])) / (vs[0] - vs[1]); } up_time += reach_time; // down LD need_len = (next_x - x) + base_reach_time * (v0 - vs[2]); LD down_x = x + time * (vs[1] - vs[2]); LD down_time = time + base_reach_time; int need_fr = max(0, int(down_x + EPS)); int need_to = down_x + need_len - EPS; bool need_to_wait = false; if (need_fr < L) need_to_wait = hole_fr[need_fr] < (need_to - need_fr + 1); if (need_to_wait) { // cerr << "down: need to wait" << endl; // cerr << "tree_find " << need_fr + 1 << " " << (int)(need_len + 1 - // EPS) // << endl; int xx = tree_find(need_fr + 1, (int)(need_len + 1 - EPS), 1, 0, NN); // cerr << "tree_find " << need_fr + 1 << " " << (int)(need_len + 1 - // EPS) // << " : " << xx << endl; auto must_wait = LD(xx - down_x) / (vs[1] - vs[2]); // cerr << "must wait " << must_wait << " until can move down" << // endl; down_time = time + base_reach_time + must_wait; } // PRINT(down_time) // PRINT(up_time) time = min(down_time, up_time); x = next_x; } // ASS(x == last + 1); // PRINT(time); LD final_time = time; FOR(i, 3) { if (i == 1) continue; int last_car = -1; FOR(j, L) if (lanes[i][j]) last_car = j; LD x_left = (last_car + 1 - x) + time * (vs[i] - vs[1]); // PRINT(x_left) REMAX(final_time, time + x_left / (v0 - vs[i])); } cout << final_time << endl; } }; int main() { ios_base::sync_with_stdio(0); cout.precision(20); cout << fixed; int num_cases = 1; if (READ_NUM_TEST_CASES) cin >> num_cases; FOR(i, num_cases) { TC tc; tc.solve(); } 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | #include <bits/stdc++.h> using namespace std; #define FOR(i, n) for (int i = 0; i < int(n); ++i) #define FO(i, a, b) for (int i = (a); i < int(b); ++i) #define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAX(a, b) ((b) < (a) ? (a) : (b)) #define REMIN(a, b) ((a) = min(a, b)) #define REMAX(a, b) ((a) = max(a, b)) #define ALL(c) (c).begin(), (c).end() #define SQR(x) ((x) * (x)) #define PRINT(x) cerr << #x << " == " << x << endl; #define ZERO(x) memset(x, 0, sizeof(x)); #define endl '\n' using LD = long double; #define ASS(x) \ if (!(x)) \ cerr << "ASSERT FAILED " #x << " " << __FILE__ << ':' << __LINE__ << endl; // const bool READ_NUM_TEST_CASES = false; const int N = 200'000 + 9; const int NN = 1024 * 256; struct Tree { int max_hole = 0; int max_pref = 0; int max_suff = 0; }; Tree tree[2 * NN]; void tree_set(int x, bool last = false) { x += NN; tree[x].max_hole = tree[x].max_pref = tree[x].max_suff = last ? INT_MAX / 2 : 1; int width = 1; while (x) { x /= 2; tree[x].max_hole = max({tree[x * 2].max_hole, tree[x * 2 + 1].max_hole, tree[x * 2].max_suff + tree[x * 2 + 1].max_pref}); tree[x].max_suff = tree[x * 2 + 1].max_hole == width ? tree[x * 2].max_suff + width : tree[x * 2 + 1].max_suff; tree[x].max_pref = tree[x * 2].max_hole == width ? tree[x * 2 + 1].max_pref + width : tree[x * 2].max_pref; width *= 2; } } int tree_find(int x0, int len, int x, int a, int b) { // cerr << "tree_find" << endl; // PRINT(x0) // PRINT(len) // PRINT(x) // PRINT(a) // PRINT(b) // cerr << endl; // this_thread::sleep_for(100ms); int mid = (a + b) / 2; if (a + 1 == b) { if (tree[x].max_hole >= len) return a; else return INT_MAX; } if (x0 >= mid) return tree_find(x0, len, x * 2 + 1, mid, b); if (tree[x * 2].max_hole >= len) { auto r = tree_find(x0, len, x * 2, a, mid); if (r != INT_MAX) return r; } REMAX(x0, mid - tree[x * 2].max_suff); if ((mid - x0) + tree[x * 2 + 1].max_pref >= len) return x0; return tree_find(x0, len, x * 2 + 1, mid, b); } struct TC { int L; int vs[3]; int v0; char lanes[3][N]; LD res[N]; int prev_free[N]; int ride_until[N]; int hole_fr[N]; void solve() { cin >> L >> v0; FOR(i, 3) cin >> vs[i]; FOR(i, 3) { string s; cin >> s; FOR(j, L) lanes[i][j] = (s[j] == '#'); lanes[i][L] = 0; } lanes[2][0] = 0; FOR(j, L) { if (lanes[0][j]) prev_free[j] = prev_free[j - 1]; else prev_free[j] = j; } ride_until[L] = L; OF(j, 0, L) { if (lanes[0][j + 1]) ride_until[j] = j; else ride_until[j] = ride_until[j + 1]; } hole_fr[L] = INT_MAX / 2; OF(j, 0, L) { if (lanes[2][j]) hole_fr[j] = 0; else hole_fr[j] = hole_fr[j + 1] + 1; } FOR(j, L) if (!lanes[2][j]) tree_set(j); tree_set(L, true); LD time = 0; int x = 0; int last = -1; FOR(j, L) if (lanes[1][j]) last = j; while (x < last + 1) { int next_x = x; while (lanes[1][next_x + 1] == 0 && next_x < last + 1) ++next_x; if (next_x != x) { time += LD(next_x - x) / (v0 - vs[1]); x = next_x; continue; } next_x += 1; while (lanes[1][next_x]) ++next_x; // cerr << endl // << "time " << time << " jump " << x << " -> " << next_x << endl; const LD EPS = 1e-12; // up LD up_time = time; LD up_time_diff = 0; LD up_x = x - time * (vs[0] - vs[1]); if (lanes[0][max(0, int(up_x + EPS))] || lanes[0][max(0, int(up_x + 1 - EPS))]) { up_time_diff = (up_x - prev_free[int(up_x + EPS)]) / (vs[0] - vs[1]); up_time += up_time_diff; up_x = x - up_time * (vs[0] - vs[1]); } LD collide_time = (ride_until[int(up_x + EPS)] - up_x) / (v0 - vs[0]); LD base_reach_time = LD(next_x - x) / (v0 - vs[1]); // PRINT(collide_time); // PRINT(base_reach_time); LD reach_time = base_reach_time + up_time_diff; // must wait in top lane if (reach_time > collide_time) { // LD collide_x = x + collide_time * (v0 - vs[1]); reach_time = collide_time + (next_x - x - collide_time * (v0 - vs[1])) / (vs[0] - vs[1]); } up_time += reach_time; // down LD need_len = (next_x - x) + base_reach_time * (v0 - vs[2]); LD down_x = x + time * (vs[1] - vs[2]); LD down_time = time + base_reach_time; int need_fr = max(0, int(down_x + EPS)); int need_to = down_x + need_len - EPS; bool need_to_wait = false; if (need_fr < L) need_to_wait = hole_fr[need_fr] < (need_to - need_fr + 1); if (need_to_wait) { // cerr << "down: need to wait" << endl; // cerr << "tree_find " << need_fr + 1 << " " << (int)(need_len + 1 - // EPS) // << endl; int xx = tree_find(need_fr + 1, (int)(need_len + 1 - EPS), 1, 0, NN); // cerr << "tree_find " << need_fr + 1 << " " << (int)(need_len + 1 - // EPS) // << " : " << xx << endl; auto must_wait = LD(xx - down_x) / (vs[1] - vs[2]); // cerr << "must wait " << must_wait << " until can move down" << // endl; down_time = time + base_reach_time + must_wait; } // PRINT(down_time) // PRINT(up_time) time = min(down_time, up_time); x = next_x; } // ASS(x == last + 1); // PRINT(time); LD final_time = time; FOR(i, 3) { if (i == 1) continue; int last_car = -1; FOR(j, L) if (lanes[i][j]) last_car = j; LD x_left = (last_car + 1 - x) + time * (vs[i] - vs[1]); // PRINT(x_left) REMAX(final_time, time + x_left / (v0 - vs[i])); } cout << final_time << endl; } }; int main() { ios_base::sync_with_stdio(0); cout.precision(20); cout << fixed; int num_cases = 1; if (READ_NUM_TEST_CASES) cin >> num_cases; FOR(i, num_cases) { TC tc; tc.solve(); } return 0; } |