#include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; // mt19937_64 gen(2137);uniform_int_distribution<int> distr(0, 1e9);auto my_rand = bind(distr, gen); // my_rand() zwraca teraz liczbe z przedzialu [a, b] #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef pair<ld, pii> tpl; const int PASY = 3; const ld eps = 1e-9; const ld ZERO = 0; vector<int> W[PASY], Z[PASY]; int V[PASY + 1] = {3, 2, 1, 4}; int current_speed[PASY]; ld current_pos[PASY]; ld current_time = 0; ld get_pos(int idx, int v) { return idx + v * current_time; } int get_wiekszy_ostro(vector<int> &X, ld pos, int v) { int p = 0; int q = sz(X) - 1; while (p != q) { int s = (p + q) / 2; ld t = get_pos(X[s], v); if (t < pos + eps) p = s + 1; else q = s; } return X[p]; } int get_wiekszy(vector<int> &X, ld pos, int v) { int p = 0; int q = sz(X) - 1; while (p != q) { int s = (p + q) / 2; ld t = get_pos(X[s], v); if (t < pos - eps) p = s + 1; else q = s; } return X[p]; } int get_mniejszy(vector<int> &X, ld pos, int v) { int p = 0; int q = sz(X) - 1; while (p != q) { int s = (p + q + 1) / 2; ld t = get_pos(X[s], v); if (t - eps > pos) q = s - 1; else p = s; } return X[p]; } void update(ld delta) { rep(i, PASY) current_pos[i] += current_speed[i] * delta; current_time += delta; } #define BLOCKED -2 #define FINISHED -3 #define IGNORE -4 #define NEGAT_DELT -5 bool eq(ld a, ld b) { return abs(a - b) < eps; } tpl get_n(int s, int t) { int x1 = get_wiekszy(W[t], current_pos[s], V[t]); int x2 = get_mniejszy(W[t], current_pos[s], V[t]); if (abs(x1 - x2) <= 1) return {ZERO, {s, t}}; if (current_speed[s] > V[t]) { int x = x1; debug(s, t, x); ld delta = (get_pos(x, V[t]) - current_pos[s]) / ld(current_speed[s] - V[t]); if (delta < -eps) return {-1, {s, NEGAT_DELT}}; return {delta, {s, t}}; } else { int x = x2; ld delta = (current_pos[s] - get_pos(x, V[t])) / ld(V[t] - current_speed[s]); if (delta < -eps) return {-1, {s, NEGAT_DELT}}; return {delta, {s, t}}; } } tpl get_n_ostro(int s, int t) { if (current_speed[s] > V[t]) { int x = get_wiekszy_ostro(W[t], current_pos[s], V[t]); debug(s, t, x, W[t]); ld delta = (get_pos(x, V[t]) - current_pos[s]) / ld(current_speed[s] - V[t]); if (delta < -eps) return {-1, {s, NEGAT_DELT}}; return {delta, {s, t}}; } return {-1, {s, IGNORE}}; } tpl get_next(int s) { if (current_speed[s] == V[s]) return {-1, {s, BLOCKED}}; if (sz(Z[s]) == 0) return {-1, {s, FINISHED}}; auto x = get_wiekszy(Z[s], current_pos[s], V[s]); ld delta = (get_pos(x, V[s]) - current_pos[s]) / ld(current_speed[s] - V[s]); if (delta < -eps) return {-1, {s, FINISHED}}; return {delta, {s, s}}; } vector<pii> all = {{0, 1}, {1, 0}, {2, 1}, {1, 2}}; void jazda() { priority_queue<tpl, vector<tpl>, greater<tpl>> Q; rep(i, PASY) Q.push(get_next(i)); for (auto [a, b] : all) { Q.push(get_n(a, b)); Q.push(get_n_ostro(a, b)); } int F_CNT = 0; while (!Q.empty()) { auto [d, x] = Q.top(); Q.pop(); auto [s, t] = x; if (t == FINISHED) F_CNT++; if (F_CNT == PASY) { cout << current_time << endl; exit(0); } // UWAGA if (d < -eps) continue; update(d); if (s == t) { current_speed[t] = V[t]; return; } else { auto &p1 = current_pos[s]; auto &p2 = current_pos[t]; if (p1 > p2 + eps) { p2 = p1; current_speed[t] = V[3]; return; } if (d > eps) return; } } // TO NIE POWINNO SIE XDARZYC update(eps); } int32_t main() { _upgrade; int l; cin >> l; cin >> V[3]; rep(i, PASY) cin >> V[i]; rep(i, PASY) { string S; cin >> S; W[i].push_back(0); if (i == 2) S[0] = '.'; rep(j, l) if (S[j] == '.') W[i].push_back(j + 1); else Z[i].push_back(j); W[i].push_back(l + 1); } rep(i, PASY) debug(W[i], Z[i]); rep(i, 2) { current_pos[i] = 0; current_speed[i] = V[3]; } current_pos[2] = 1; current_speed[2] = V[3]; while (true) jazda(); }
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 | #include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; // mt19937_64 gen(2137);uniform_int_distribution<int> distr(0, 1e9);auto my_rand = bind(distr, gen); // my_rand() zwraca teraz liczbe z przedzialu [a, b] #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef pair<ld, pii> tpl; const int PASY = 3; const ld eps = 1e-9; const ld ZERO = 0; vector<int> W[PASY], Z[PASY]; int V[PASY + 1] = {3, 2, 1, 4}; int current_speed[PASY]; ld current_pos[PASY]; ld current_time = 0; ld get_pos(int idx, int v) { return idx + v * current_time; } int get_wiekszy_ostro(vector<int> &X, ld pos, int v) { int p = 0; int q = sz(X) - 1; while (p != q) { int s = (p + q) / 2; ld t = get_pos(X[s], v); if (t < pos + eps) p = s + 1; else q = s; } return X[p]; } int get_wiekszy(vector<int> &X, ld pos, int v) { int p = 0; int q = sz(X) - 1; while (p != q) { int s = (p + q) / 2; ld t = get_pos(X[s], v); if (t < pos - eps) p = s + 1; else q = s; } return X[p]; } int get_mniejszy(vector<int> &X, ld pos, int v) { int p = 0; int q = sz(X) - 1; while (p != q) { int s = (p + q + 1) / 2; ld t = get_pos(X[s], v); if (t - eps > pos) q = s - 1; else p = s; } return X[p]; } void update(ld delta) { rep(i, PASY) current_pos[i] += current_speed[i] * delta; current_time += delta; } #define BLOCKED -2 #define FINISHED -3 #define IGNORE -4 #define NEGAT_DELT -5 bool eq(ld a, ld b) { return abs(a - b) < eps; } tpl get_n(int s, int t) { int x1 = get_wiekszy(W[t], current_pos[s], V[t]); int x2 = get_mniejszy(W[t], current_pos[s], V[t]); if (abs(x1 - x2) <= 1) return {ZERO, {s, t}}; if (current_speed[s] > V[t]) { int x = x1; debug(s, t, x); ld delta = (get_pos(x, V[t]) - current_pos[s]) / ld(current_speed[s] - V[t]); if (delta < -eps) return {-1, {s, NEGAT_DELT}}; return {delta, {s, t}}; } else { int x = x2; ld delta = (current_pos[s] - get_pos(x, V[t])) / ld(V[t] - current_speed[s]); if (delta < -eps) return {-1, {s, NEGAT_DELT}}; return {delta, {s, t}}; } } tpl get_n_ostro(int s, int t) { if (current_speed[s] > V[t]) { int x = get_wiekszy_ostro(W[t], current_pos[s], V[t]); debug(s, t, x, W[t]); ld delta = (get_pos(x, V[t]) - current_pos[s]) / ld(current_speed[s] - V[t]); if (delta < -eps) return {-1, {s, NEGAT_DELT}}; return {delta, {s, t}}; } return {-1, {s, IGNORE}}; } tpl get_next(int s) { if (current_speed[s] == V[s]) return {-1, {s, BLOCKED}}; if (sz(Z[s]) == 0) return {-1, {s, FINISHED}}; auto x = get_wiekszy(Z[s], current_pos[s], V[s]); ld delta = (get_pos(x, V[s]) - current_pos[s]) / ld(current_speed[s] - V[s]); if (delta < -eps) return {-1, {s, FINISHED}}; return {delta, {s, s}}; } vector<pii> all = {{0, 1}, {1, 0}, {2, 1}, {1, 2}}; void jazda() { priority_queue<tpl, vector<tpl>, greater<tpl>> Q; rep(i, PASY) Q.push(get_next(i)); for (auto [a, b] : all) { Q.push(get_n(a, b)); Q.push(get_n_ostro(a, b)); } int F_CNT = 0; while (!Q.empty()) { auto [d, x] = Q.top(); Q.pop(); auto [s, t] = x; if (t == FINISHED) F_CNT++; if (F_CNT == PASY) { cout << current_time << endl; exit(0); } // UWAGA if (d < -eps) continue; update(d); if (s == t) { current_speed[t] = V[t]; return; } else { auto &p1 = current_pos[s]; auto &p2 = current_pos[t]; if (p1 > p2 + eps) { p2 = p1; current_speed[t] = V[3]; return; } if (d > eps) return; } } // TO NIE POWINNO SIE XDARZYC update(eps); } int32_t main() { _upgrade; int l; cin >> l; cin >> V[3]; rep(i, PASY) cin >> V[i]; rep(i, PASY) { string S; cin >> S; W[i].push_back(0); if (i == 2) S[0] = '.'; rep(j, l) if (S[j] == '.') W[i].push_back(j + 1); else Z[i].push_back(j); W[i].push_back(l + 1); } rep(i, PASY) debug(W[i], Z[i]); rep(i, 2) { current_pos[i] = 0; current_speed[i] = V[3]; } current_pos[2] = 1; current_speed[2] = V[3]; while (true) jazda(); } |