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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define e1 first
#define e2 second
#define pb push_back
#define OUT(x) {cout << x << "\n"; exit(0); }
#define TCOUT(x) {cout << x << "\n"; return; }
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define sz(x) int(x.size())
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

mt19937_64 rng(time(0));
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
typedef long double ld;

//Did you REAALLY consider sample tests?
const bool MYDEBUG = 0;

struct car {
	ld x; ld v;
	int len;
	car(ld xx, ld vv, int lenn) : x(xx), v(vv), len(lenn) {
	}
	ld pos(ld t) {
		return x + t * v;
	};
	pair<ld, ld> get_interval(ld t) {
		ld pocz = pos(t);
		return make_pair(pocz, pocz + len);
	}
	void print() {
		if (MYDEBUG) cerr << "Auto of length " << len << " at position " << x << " with velocity " << v << endl;
	}
};

vector <car> pas[4];
ld vpas[4];
int L;

// 0 if they intersect
// -1 if my car in front
// 1 if my car behind
const ld EPS = 0.0000000005;

int cmp(const ld &x, const pair<ld, ld> &other) { //always resolve ties in favour of not colliding
	if (x + 1 - EPS <= other.first) return -1;
	if (x + EPS >= other.second) return 1;
	return 0;
}

bool is_free(int numer_pasa, ld t, ld x) {
	if (pas[numer_pasa].empty()) return true;
	int xx = 0, yy = sz(pas[numer_pasa]) - 1;
	while (xx < yy) {
		int mid = (xx + yy) / 2;
		auto car_interval = pas[numer_pasa][mid].get_interval(t);
		int cc = cmp(x, car_interval);
		if (cc == 0) return false;
		if (cc == -1) yy = --mid;
		else xx = ++mid;
	}
	
	auto car_interval = pas[numer_pasa][xx].get_interval(t);
	int cc = cmp(x, car_interval);
	return cc != 0;
}

int find_next_car(int numer_pasa, ld t, ld x) { //pierwsze auto przede mna, czyli find first car dla ktorego NIE jest 1
	if (pas[numer_pasa].empty()) return -1;
	int xx = 0, yy = sz(pas[numer_pasa]) - 1;
	while (xx < yy) {
		int mid = (xx + yy) / 2;
		auto car_interval = pas[numer_pasa][mid].get_interval(t);
		int cc = cmp(x, car_interval);
		if (cc == 1) xx = ++mid;
		else yy = mid;
	}
	
	auto car_interval = pas[numer_pasa][xx].get_interval(t);
	int cc = cmp(x, car_interval);
	if (cc == 1) {
		return -1;
	}
	else {
		return xx;
	}
}

string grid[4];
ld X[4], V[4];
const ld INF = 1e15;
const ld NOCARINF = 1e16;

bool TEST = 0;
pair<ld, ld> overtake[4][4];

vector <bool> can_still_undertake[4];
int OPTION[4][4];

int sgn(ld value) {
	if (value > 0) return 1;
	if (value < 0) return -1;
	return 0;
}

int main() {
	boost;
	cout << fixed << setprecision(15);
	cin >> L;
	rep(i, 0, 4) cin >> vpas[i];
	rep(i, 1, 4) {
		cin >> grid[i];
		grid[i] = "." + grid[i] + ".";
		for (int j=sz(grid[i])-2; j>=0; --j) {
			if (grid[i][j] == '#' && grid[i][j+1] == '.') {
				int x = j;
				while (grid[i][x-1] == '#') --x;
				if (i == 3 && x == 1) ++x;
				if (x <= j) {
					pas[i].pb(car(x, vpas[i], j - x + 1));
					can_still_undertake[i].pb(true);
				}
			}
		}
	}
	
	rep(i, 1, 4) reverse(all(pas[i]));
	/*rep(i, 1, 4) {
		debug(i);
		for (auto samochod : pas[i]) samochod.print();
	}*/
	
	ld t = 0.0;
	rep(i, 1, 4) {
		X[i] = 1.0;
		V[i] = vpas[0];
	}
	
	while (true) {
		if (MYDEBUG) {
			cerr << "\nSimulation time: " << t << endl;
			debug(X[1], X[2], X[3]);
			debug(V[1], V[2], V[3]);
			debug("Position of all cars");
			FOR(i, 1, 3) {
				trav(u, pas[i]) cout << u.pos(t) << ' ';
				cout << endl;
			}
		}
		
		//najpierw sprawdzmy, czy da sie zmienic pas bezpiecznie
		auto transition = [&](int skad, int dokad) {
			if (X[skad] <= X[dokad]) return; //we cannot have a better option
			///debug("Try", skad, dokad);
			if (is_free(dokad, t, X[skad])) {
				if (MYDEBUG) cerr << "Changing from lane " << skad << " to lane " << dokad << " at time: " << t << endl;
				V[dokad] = vpas[0];
				X[dokad] = X[skad];
				if (MYDEBUG) {
					debug(X[1], X[2], X[3]);
					debug(V[1], V[2], V[3]);
				}
			}
		};
		
		auto time_of_approaching_next_car = [&](int pass) {
			if ((int)vpas[pass] == (int)V[pass]) return INF; //predkosci te same - nigdy to nie nastapi
			int car_place = find_next_car(pass, t, X[pass]);
			if (car_place == -1) return NOCARINF; //nigdy to nie nastapi
			car that_car = pas[pass][car_place];
			//that_car.print();
			ld car_difference = that_car.pos(t) - 1 - X[pass];
			if (abs(car_difference) < EPS) return t;
			return t + (car_difference / (V[pass] - vpas[pass]));
		};
		
		transition(1, 2);
		transition(3, 2); //at this point 2 is surely optimal
		transition(2, 1);
		transition(2, 3); //now 1 and 3 are optimal as well
		
		// this took 0 time, now let's focus on events which can take time
		ld next_event_time = NOCARINF;
		vector <ld> w_plecy(4); //kiedy wjade mojemu ziomkowi w plecy

		rep(i, 1, 4) {
			w_plecy[i] = time_of_approaching_next_car(i);
			if (w_plecy[i] == t) {
				V[i] = vpas[i];
			}
			next_event_time = min(next_event_time, w_plecy[i]);
		}
		
		if (MYDEBUG) {
			debug(w_plecy);
		}
		if (next_event_time == t) continue;
		if (next_event_time == NOCARINF) {
			cout << t << "\n";
			exit(0);
		}
		
		//there is some car which we can try to pass
		auto overtake_car = [&](int skad, int dokad) -> pair<ld, ld> {
			OPTION[skad][dokad] = -1;
			int car_place = find_next_car(dokad, t, X[skad]);
			if (car_place == -1) return {NOCARINF, NOCARINF};
			car that_car = pas[dokad][car_place];
			//that_car.print();
			ld car_finish = that_car.pos(t) + that_car.len;
			ld car_difference = car_finish - X[skad];
			ld my_velocity = V[skad] - vpas[dokad];
			// speeding znalazlem buga!!
			if (my_velocity <= 0) { //nigdy nie wyprzedze, ale moge sprobowac zostac wyprzedzonym przez to auto w calosci!!!
				//if (V[skad] == vpas[0]) return {INF, INF}; //impossible to overtake if I am going with max speed
				auto car_interval = that_car.get_interval(t);
				int cc = cmp(X[skad], car_interval);
				if (cc != 0) return {INF, INF}; //not intersecting -> which means this is free and we should not wait
				///debug("Considering undertake", skad, dokad);
				ld desired_position = that_car.pos(t) - 1;
				car_difference = desired_position - X[skad];
				if (car_difference > 0) return {INF, INF};
				car_difference *= -1;
				
				ld time_until_previous_hits_me = INF;
				ld distance_from_previous_car = INF;
				int next_car_this_lane = find_next_car(skad, t, X[skad]);
				if (next_car_this_lane == -1) next_car_this_lane = sz(pas[skad]);
				
				if (!can_still_undertake[dokad][car_place]) return {INF, INF};
				OPTION[skad][dokad] = car_place;
				can_still_undertake[skad][next_car_this_lane] = false;
				
				if (next_car_this_lane > 0) {
					distance_from_previous_car = X[skad] - pas[skad][next_car_this_lane - 1].pos(t) - pas[skad][next_car_this_lane - 1].len;
					time_until_previous_hits_me = distance_from_previous_car / vpas[skad];
				}
				
				ld czekajac_zerem = car_difference / vpas[dokad];
				if (czekajac_zerem <= time_until_previous_hits_me) {
					ld ret_value = min(INF, t + czekajac_zerem);
					return {that_car.pos(ret_value) - 1, ret_value};
				}
				if (MYDEBUG) debug(time_until_previous_hits_me, distance_from_previous_car, czekajac_zerem);
				
				// juz wiecej nie moge jechac samym zerem
				car_difference -= time_until_previous_hits_me * vpas[dokad];
				ld delta = car_difference / (vpas[dokad] - vpas[skad]);
				ld ret_value = min(INF, t + time_until_previous_hits_me + delta);
				return {that_car.pos(ret_value) - 1, ret_value};
			}
			
			ld delta = (car_difference / my_velocity);
			ld ret_value = min(INF, t + delta);
			return {that_car.pos(ret_value) + that_car.len, ret_value};
		};
		
		rep(i, 1, 4) {
			rep(j, 1, 4) {
				if (abs(i - j) == 1) {
					overtake[i][j] = overtake_car(i, j);
					if (MYDEBUG) debug(i, j, overtake[i][j]);
					next_event_time = min(next_event_time, overtake[i][j].second);
				}
			}
		}
		
		auto the_other = [&](int a, int b) {
			if (a > b) swap(a, b);
			if (a == 1 && b == 2) return 3;
			if (a == 1 && b == 3) return 2;
			if (a == 2 && b == 3) return 1;
			assert(1 == 0);
		};
		
		bool overtakes = false;
		ld time_delta = next_event_time - t;
		assert(next_event_time != INF);
		next_event_time = max(next_event_time, t);
		
		if (false) {
			cout << "PANIC\n";
			cout << next_event_time << ' ' << t << endl;
			cerr << "\nSimulation time: " << t << endl;
			debug(X[1], X[2], X[3]);
			debug(V[1], V[2], V[3]);
			debug("Position of all cars");
			FOR(i, 1, 3) {
				trav(u, pas[i]) cout << u.pos(t) << ' ';
				cout << endl;
			}
			exit(0);
		}
		
		rep(i, 1, 4) {
			rep(j, 1, 4) {
				if (abs(i - j) == 1) {
					overtake[i][j].second = max(t, overtake[i][j].second);
					ld proposed_time_delta = overtake[i][j].second - t; 
					//debug(proposed_time_delta);
					//cerr << overtake[i][j].first << ' ' << X[j] + V[j] * proposed_time_delta << endl;
					if (!overtakes && overtake[i][j].second == next_event_time) {
						if (MYDEBUG) {
							debug("Deciding to overtake?", i, j);
							debug(overtake[i][j].first, X[j], V[j]);
							debug(X[j] + V[j] * proposed_time_delta);
						}
						if (overtake[i][j].first <= X[j] + V[j] * proposed_time_delta) continue; //not overtaking better positions
						if (MYDEBUG) debug("Decided.");
						overtakes = true;
						if (OPTION[i][j] != -1) {
							if (MYDEBUG) debug("In fact it was undertaking");
							can_still_undertake[j][OPTION[i][j]] = false;
						}
						if (MYDEBUG) cerr << "Overtaking from lane " << i << " to lane " << j << " at time: " << overtake[i][j].second << endl;
						X[j] = overtake[i][j].first;
						X[i] += time_delta * V[i]; //they can differ significantly if it was an UNDERTAKE -> X[i] should NOT propagate
	
						int other = the_other(i, j);
						X[other] += time_delta * V[other];
						V[j] = vpas[0]; //wlasnie wyprzedzilem auto, wiec speed jest v0
						if (MYDEBUG) {
							debug(X[1], X[2], X[3]);
							debug(V[1], V[2], V[3]);
						}
					}
				}
			}
		}
		
		if (!overtakes) {
			FOR(i, 1, 3) X[i] += time_delta * V[i];
		}
		
		t = next_event_time;
	}
}