#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif using T = LL; T abs(T x) { if (x < 0) return -x; return x; } T nwd(T a, T b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b != 0) { T temp = a; a = b; b = temp % b; } return a; } struct Frac { T nom, denom; // !!!!!!!!!!!!!!!!!!!!!! RACZEJ DO ZMIANY NA __INT_128_T !!!!!!!!!!!!!!!!!!!!!!! Frac() : nom(0), denom(1) {} Frac(T x) : nom(x), denom(1) {} Frac(T _nom, T _denom) : nom(_nom), denom(_denom) { this->normalize(); } void normalize() { T d = nwd(this->nom, this->denom); this->nom /= d; this->denom /= d; if (this->denom < 0) { this->nom = -this->nom; this->denom = -this->denom; } } const Frac operator+(const Frac& other) const { T common_denom = this->denom / nwd(this->denom, other.denom) * other.denom; T new_nom = this->nom * (common_denom / this->denom) + other.nom * (common_denom / other.denom); T new_denom = common_denom; return Frac(new_nom, new_denom); } const Frac operator-(const Frac& other) const { T common_denom = this->denom / nwd(this->denom, other.denom) * other.denom; T new_nom = this->nom * (common_denom / this->denom) - other.nom * (common_denom / other.denom); T new_denom = common_denom; return Frac(new_nom, new_denom); } const Frac operator*(const Frac& other) const { T d1 = nwd(this->nom, other.denom); T d2 = nwd(this->denom, other.nom); T new_nom = this->nom / d1 * (other.nom / d2); T new_denom = this->denom / d2 * (other.denom / d1); return Frac(new_nom, new_denom); } const Frac inv() const { return Frac(this->denom, this->nom); } const Frac operator/(const Frac& other) const { return (*this) * other.inv(); } friend ostream& operator <<(ostream& os, const Frac frac) { //os << '(' << frac.nom << " / " << frac.denom << ')'; return os; } long double output() { return (long double)this->nom / (long double)this->denom; } auto operator==(const Frac& other) const { Frac diff = (*this) - other; return diff.nom == 0; } auto operator<(const Frac& other) const { Frac diff = (*this) - other; return diff.nom < 0; } auto operator<=(const Frac& other) const { Frac diff = (*this) - other; return diff.nom <= 0; } auto operator>(const Frac& other) const { Frac diff = (*this) - other; return diff.nom > 0; } auto operator>=(const Frac& other) const { Frac diff = (*this) - other; return diff.nom >= 0; } auto operator!=(const Frac& other) const { Frac diff = (*this) - other; return diff.nom != 0; } }; int main() { cin.tie(0)->sync_with_stdio(0); const LL INF = (1ll << 30); int L; cin >> L; vector<Frac> v(4); REP(i,4) { int x; cin >> x; v[i] = Frac(x); } debug(L, v); vector<vector<char>> s(3, vector<char> (L + 1)); REP(i,3) { REP(j,L) { cin >> s[i][j]; } s[i][L] = '.'; } s[2][0] = '.'; debug(s[0]); debug(s[1]); debug(s[2]); vector<vector<pair<LL,LL>>> p(3); REP(i,3) { REP(j,L+1) { if (s[i][j] == '#') { continue; } int x = j; while (x + 1 <= L && s[i][x + 1] == '.') { ++x; } p[i].emplace_back(pair{j, x}); j = x; } p[i][ssize(p[i]) - 1].second = INF; } debug(p[0]); debug(p[1]); debug(p[2]); auto pos = [&](int i, LL j, const Frac& time) { return Frac(j) + v[i + 1] * time; }; int r0 = ssize(p[0]); int r = ssize(p[1]); int r2 = ssize(p[2]); debug(r0, r, r2); vector<Frac> dp(r, Frac(INF)); dp[0] = Frac(0); int wsk0 = -1; int wsk2 = 0; FOR(i,1,r-1) { debug("start", i); auto cur_time = dp[i - 1] + Frac(p[1][i - 1].second - p[1][i - 1].first) / (v[0] - v[2]); auto cur_pos = pos(1, p[1][i - 1].first, dp[i - 1]) + (cur_time - dp[i - 1]) * v[0]; debug(cur_time); if (wsk0 + 1 < r0) { debug(cur_pos, pos(0, p[0][wsk0 + 1].first, cur_time)); } while (wsk0 + 1 < r0 && pos(0, p[0][wsk0 + 1].first, cur_time) <= cur_pos) { ++wsk0; } debug(cur_pos, pos(0, p[0][wsk0].second, cur_time)); debug((cur_pos - pos(0, p[0][wsk0].second, cur_time)), v[1] - v[2]); auto waiting_time = max(Frac(0), (cur_pos - pos(0, p[0][wsk0].second, cur_time)) / (v[1] - v[2])); cur_time = cur_time + waiting_time; cur_pos = cur_pos + waiting_time * v[2]; debug(waiting_time, cur_time, cur_pos); auto no_stopping = Frac(p[1][i].first - p[1][i - 1].second) / (v[0] - v[2]); debug(pos(0, p[0][wsk0].second, cur_time), cur_pos); auto until_contact = (pos(0, p[0][wsk0].second, cur_time) - cur_pos) / (v[0] - v[1]); Frac cur_best; if (no_stopping <= until_contact) { cur_best = cur_time + no_stopping; } else { auto new_pos = cur_pos + until_contact * v[0]; auto new_time = cur_time + until_contact; auto new_target = pos(1, p[1][i].first, new_time); cur_best = new_time + (new_target - new_pos) / (v[1] - v[2]); } debug(i, wsk0, cur_best); dp[i] = cur_best; debug("wsk2"); cur_time = dp[i - 1] + Frac(p[1][i - 1].second - p[1][i - 1].first) / (v[0] - v[2]); cur_pos = pos(1, p[1][i - 1].first, dp[i - 1]) + (cur_time - dp[i - 1]) * v[0]; debug(cur_time, cur_pos); int temp_wsk2 = wsk2; while (temp_wsk2 < r2) { auto back = pos(2, p[2][temp_wsk2].second, cur_time); if (back < cur_pos) { ++wsk2; ++temp_wsk2; continue; } auto waiting_time2 = max(Frac(0), (pos(2, p[2][temp_wsk2].first, cur_time) - cur_pos) / (v[2] - v[3])); auto new_time2 = cur_time + waiting_time2; auto new_pos = cur_pos + waiting_time2 * v[2]; debug(waiting_time2, new_time2, new_pos); auto bound = new_time2 + Frac(p[1][i].first - p[1][i - 1].second) / (v[0] - v[2]); debug(bound); if (bound >= cur_best) { break; } debug(temp_wsk2); auto until_contact2 = (pos(2, p[2][temp_wsk2].second, new_time2) - new_pos) / (v[0] - v[3]); debug(bound, until_contact2); if (bound <= until_contact2 + new_time2) { dp[i] = bound; wsk2 = temp_wsk2; break; } wsk2 = temp_wsk2; // chyba ++temp_wsk2; } debug("end", dp[i]); } debug(dp); cout << setprecision(15) << fixed; auto ans = dp[r - 1]; auto cur = pos(1, p[1].back().first, dp[r - 1]); REP(i,3) { if (i == 1) continue; auto other_car = pos(i, p[i].back().first, dp[r - 1]); debug(cur, other_car, dp[r - 1] + (other_car - cur) / (v[0] - v[i + 1])); ans = max(ans, dp[r - 1] + (other_car - cur) / (v[0] - v[i + 1])); } debug(ans); cout << ans.output() << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif using T = LL; T abs(T x) { if (x < 0) return -x; return x; } T nwd(T a, T b) { if (a < 0) a = -a; if (b < 0) b = -b; while (b != 0) { T temp = a; a = b; b = temp % b; } return a; } struct Frac { T nom, denom; // !!!!!!!!!!!!!!!!!!!!!! RACZEJ DO ZMIANY NA __INT_128_T !!!!!!!!!!!!!!!!!!!!!!! Frac() : nom(0), denom(1) {} Frac(T x) : nom(x), denom(1) {} Frac(T _nom, T _denom) : nom(_nom), denom(_denom) { this->normalize(); } void normalize() { T d = nwd(this->nom, this->denom); this->nom /= d; this->denom /= d; if (this->denom < 0) { this->nom = -this->nom; this->denom = -this->denom; } } const Frac operator+(const Frac& other) const { T common_denom = this->denom / nwd(this->denom, other.denom) * other.denom; T new_nom = this->nom * (common_denom / this->denom) + other.nom * (common_denom / other.denom); T new_denom = common_denom; return Frac(new_nom, new_denom); } const Frac operator-(const Frac& other) const { T common_denom = this->denom / nwd(this->denom, other.denom) * other.denom; T new_nom = this->nom * (common_denom / this->denom) - other.nom * (common_denom / other.denom); T new_denom = common_denom; return Frac(new_nom, new_denom); } const Frac operator*(const Frac& other) const { T d1 = nwd(this->nom, other.denom); T d2 = nwd(this->denom, other.nom); T new_nom = this->nom / d1 * (other.nom / d2); T new_denom = this->denom / d2 * (other.denom / d1); return Frac(new_nom, new_denom); } const Frac inv() const { return Frac(this->denom, this->nom); } const Frac operator/(const Frac& other) const { return (*this) * other.inv(); } friend ostream& operator <<(ostream& os, const Frac frac) { //os << '(' << frac.nom << " / " << frac.denom << ')'; return os; } long double output() { return (long double)this->nom / (long double)this->denom; } auto operator==(const Frac& other) const { Frac diff = (*this) - other; return diff.nom == 0; } auto operator<(const Frac& other) const { Frac diff = (*this) - other; return diff.nom < 0; } auto operator<=(const Frac& other) const { Frac diff = (*this) - other; return diff.nom <= 0; } auto operator>(const Frac& other) const { Frac diff = (*this) - other; return diff.nom > 0; } auto operator>=(const Frac& other) const { Frac diff = (*this) - other; return diff.nom >= 0; } auto operator!=(const Frac& other) const { Frac diff = (*this) - other; return diff.nom != 0; } }; int main() { cin.tie(0)->sync_with_stdio(0); const LL INF = (1ll << 30); int L; cin >> L; vector<Frac> v(4); REP(i,4) { int x; cin >> x; v[i] = Frac(x); } debug(L, v); vector<vector<char>> s(3, vector<char> (L + 1)); REP(i,3) { REP(j,L) { cin >> s[i][j]; } s[i][L] = '.'; } s[2][0] = '.'; debug(s[0]); debug(s[1]); debug(s[2]); vector<vector<pair<LL,LL>>> p(3); REP(i,3) { REP(j,L+1) { if (s[i][j] == '#') { continue; } int x = j; while (x + 1 <= L && s[i][x + 1] == '.') { ++x; } p[i].emplace_back(pair{j, x}); j = x; } p[i][ssize(p[i]) - 1].second = INF; } debug(p[0]); debug(p[1]); debug(p[2]); auto pos = [&](int i, LL j, const Frac& time) { return Frac(j) + v[i + 1] * time; }; int r0 = ssize(p[0]); int r = ssize(p[1]); int r2 = ssize(p[2]); debug(r0, r, r2); vector<Frac> dp(r, Frac(INF)); dp[0] = Frac(0); int wsk0 = -1; int wsk2 = 0; FOR(i,1,r-1) { debug("start", i); auto cur_time = dp[i - 1] + Frac(p[1][i - 1].second - p[1][i - 1].first) / (v[0] - v[2]); auto cur_pos = pos(1, p[1][i - 1].first, dp[i - 1]) + (cur_time - dp[i - 1]) * v[0]; debug(cur_time); if (wsk0 + 1 < r0) { debug(cur_pos, pos(0, p[0][wsk0 + 1].first, cur_time)); } while (wsk0 + 1 < r0 && pos(0, p[0][wsk0 + 1].first, cur_time) <= cur_pos) { ++wsk0; } debug(cur_pos, pos(0, p[0][wsk0].second, cur_time)); debug((cur_pos - pos(0, p[0][wsk0].second, cur_time)), v[1] - v[2]); auto waiting_time = max(Frac(0), (cur_pos - pos(0, p[0][wsk0].second, cur_time)) / (v[1] - v[2])); cur_time = cur_time + waiting_time; cur_pos = cur_pos + waiting_time * v[2]; debug(waiting_time, cur_time, cur_pos); auto no_stopping = Frac(p[1][i].first - p[1][i - 1].second) / (v[0] - v[2]); debug(pos(0, p[0][wsk0].second, cur_time), cur_pos); auto until_contact = (pos(0, p[0][wsk0].second, cur_time) - cur_pos) / (v[0] - v[1]); Frac cur_best; if (no_stopping <= until_contact) { cur_best = cur_time + no_stopping; } else { auto new_pos = cur_pos + until_contact * v[0]; auto new_time = cur_time + until_contact; auto new_target = pos(1, p[1][i].first, new_time); cur_best = new_time + (new_target - new_pos) / (v[1] - v[2]); } debug(i, wsk0, cur_best); dp[i] = cur_best; debug("wsk2"); cur_time = dp[i - 1] + Frac(p[1][i - 1].second - p[1][i - 1].first) / (v[0] - v[2]); cur_pos = pos(1, p[1][i - 1].first, dp[i - 1]) + (cur_time - dp[i - 1]) * v[0]; debug(cur_time, cur_pos); int temp_wsk2 = wsk2; while (temp_wsk2 < r2) { auto back = pos(2, p[2][temp_wsk2].second, cur_time); if (back < cur_pos) { ++wsk2; ++temp_wsk2; continue; } auto waiting_time2 = max(Frac(0), (pos(2, p[2][temp_wsk2].first, cur_time) - cur_pos) / (v[2] - v[3])); auto new_time2 = cur_time + waiting_time2; auto new_pos = cur_pos + waiting_time2 * v[2]; debug(waiting_time2, new_time2, new_pos); auto bound = new_time2 + Frac(p[1][i].first - p[1][i - 1].second) / (v[0] - v[2]); debug(bound); if (bound >= cur_best) { break; } debug(temp_wsk2); auto until_contact2 = (pos(2, p[2][temp_wsk2].second, new_time2) - new_pos) / (v[0] - v[3]); debug(bound, until_contact2); if (bound <= until_contact2 + new_time2) { dp[i] = bound; wsk2 = temp_wsk2; break; } wsk2 = temp_wsk2; // chyba ++temp_wsk2; } debug("end", dp[i]); } debug(dp); cout << setprecision(15) << fixed; auto ans = dp[r - 1]; auto cur = pos(1, p[1].back().first, dp[r - 1]); REP(i,3) { if (i == 1) continue; auto other_car = pos(i, p[i].back().first, dp[r - 1]); debug(cur, other_car, dp[r - 1] + (other_car - cur) / (v[0] - v[i + 1])); ans = max(ans, dp[r - 1] + (other_car - cur) / (v[0] - v[i + 1])); } debug(ans); cout << ans.output() << '\n'; } |