#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctz(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 18; struct Truck { int front, back, v; Truck(int f=0, int b=0, int w=0) : front(f), back(b), v(w) {} }Karol; int& pos = Karol.front; queue <Truck> T[4]; char buff[maxN]; int V[4]; long long PP = 1; void parse() { int n; scanf ("%d", &n); FOR(i, 0, 4) scanf ("%d", V+i); FOR(i, 0, 4) if (i != 2) V[i] -= V[2], PP *= V[i]; V[2] = 0; Karol.v = V[0]; FOR(s, 1, 4) { scanf ("%s", buff); buff[0] = '.'; int back; FOR(i, 1, n) { if (buff[i] == '#' and buff[i-1] == '.') back = i; if (buff[i] == '#' and (i+1 == n or buff[i+1] == '.')) T[s].push(Truck(i, back, V[s])); } } } struct Rat { map <int, long long> M; Rat() { FOR(s, 0, 4) if (s != 2) M[V[s]] = 0; } Rat(long long p, long long q) : Rat() { M[q] = p; } void operator+= (const Rat& other) { for (auto& it : other.M) M[it.ft] += it.sd; } Rat operator+ (const Rat& other) { Rat ret = *this; ret += other; return ret; } long long rep() const { long long ret = 0; for (auto it : M) ret += PP / it.ft * it.sd; return ret; } bool operator<= (const Rat& other) const { return rep() >= other.rep(); } bool operator>= (const Rat& other) const { return other <= (*this); } bool operator< (const Rat& other) const { return rep() > other.rep(); } long double operator() () { long double ret = 0; for (auto it : M) { long long a = it.ft, b = it.sd; long long c = __gcd((long long)a, b); a /= c, b /= c; ret += 1.0 * b / a; } return ret; } }; Rat travelTime(int from, int to, int vv) { return Rat(to - from, vv); } Rat travelTime(Truck& x, int to, bool bk = false) { return travelTime(bk ? x.back : x.front, to, x.v); } const bool BACK_MD = true; Rat Left(int dest, Rat t) { queue <Truck>& Q = T[1]; while (!Q.empty() and t <= travelTime(Q.front(), pos - 1)) Q.pop(); t += travelTime(Karol, dest); if (!Q.empty()) remax(t, travelTime(Q.front(), dest + 1, BACK_MD)); return t; } Rat Right(int dest, Rat t, const Rat& border) { queue <Truck>& Q = T[3]; while (!Q.empty() and t >= travelTime(Q.front(), pos-1)) Q.pop(); Rat mydt = travelTime(Karol, dest); while (!Q.empty()) { if (t + mydt <= travelTime(Q.front(), dest+1, BACK_MD)) break; t = travelTime(Q.front(), pos-1); if (t <= border) Q.pop(); else break; } return t + mydt; } void solve() { parse(); Rat t; for (; !T[2].empty(); T[2].pop()) { auto x = T[2].front(); t += travelTime(Karol, x.back - 1); pos = x.back - 1; int dest = x.front + 1; Rat tl = Left(dest, t); Rat tr = Right(dest, t, tl); t = min(tl, tr); pos = dest; } long double tt = t(), t0 = tt; FOR(s, 1, 4) { queue <Truck>& Q = T[s]; while (SZ(Q) > 1) Q.pop(); if (!Q.empty()) // s != 2 { auto x = Q.front(); int v = Karol.v - x.v; long double where = x.front + x.v * t0; long double race = 1.0 * (where+1 - pos) / v; remax(tt, t0 + race); } } printf("%.15Lf\n", tt); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); 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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctz(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 18; struct Truck { int front, back, v; Truck(int f=0, int b=0, int w=0) : front(f), back(b), v(w) {} }Karol; int& pos = Karol.front; queue <Truck> T[4]; char buff[maxN]; int V[4]; long long PP = 1; void parse() { int n; scanf ("%d", &n); FOR(i, 0, 4) scanf ("%d", V+i); FOR(i, 0, 4) if (i != 2) V[i] -= V[2], PP *= V[i]; V[2] = 0; Karol.v = V[0]; FOR(s, 1, 4) { scanf ("%s", buff); buff[0] = '.'; int back; FOR(i, 1, n) { if (buff[i] == '#' and buff[i-1] == '.') back = i; if (buff[i] == '#' and (i+1 == n or buff[i+1] == '.')) T[s].push(Truck(i, back, V[s])); } } } struct Rat { map <int, long long> M; Rat() { FOR(s, 0, 4) if (s != 2) M[V[s]] = 0; } Rat(long long p, long long q) : Rat() { M[q] = p; } void operator+= (const Rat& other) { for (auto& it : other.M) M[it.ft] += it.sd; } Rat operator+ (const Rat& other) { Rat ret = *this; ret += other; return ret; } long long rep() const { long long ret = 0; for (auto it : M) ret += PP / it.ft * it.sd; return ret; } bool operator<= (const Rat& other) const { return rep() >= other.rep(); } bool operator>= (const Rat& other) const { return other <= (*this); } bool operator< (const Rat& other) const { return rep() > other.rep(); } long double operator() () { long double ret = 0; for (auto it : M) { long long a = it.ft, b = it.sd; long long c = __gcd((long long)a, b); a /= c, b /= c; ret += 1.0 * b / a; } return ret; } }; Rat travelTime(int from, int to, int vv) { return Rat(to - from, vv); } Rat travelTime(Truck& x, int to, bool bk = false) { return travelTime(bk ? x.back : x.front, to, x.v); } const bool BACK_MD = true; Rat Left(int dest, Rat t) { queue <Truck>& Q = T[1]; while (!Q.empty() and t <= travelTime(Q.front(), pos - 1)) Q.pop(); t += travelTime(Karol, dest); if (!Q.empty()) remax(t, travelTime(Q.front(), dest + 1, BACK_MD)); return t; } Rat Right(int dest, Rat t, const Rat& border) { queue <Truck>& Q = T[3]; while (!Q.empty() and t >= travelTime(Q.front(), pos-1)) Q.pop(); Rat mydt = travelTime(Karol, dest); while (!Q.empty()) { if (t + mydt <= travelTime(Q.front(), dest+1, BACK_MD)) break; t = travelTime(Q.front(), pos-1); if (t <= border) Q.pop(); else break; } return t + mydt; } void solve() { parse(); Rat t; for (; !T[2].empty(); T[2].pop()) { auto x = T[2].front(); t += travelTime(Karol, x.back - 1); pos = x.back - 1; int dest = x.front + 1; Rat tl = Left(dest, t); Rat tr = Right(dest, t, tl); t = min(tl, tr); pos = dest; } long double tt = t(), t0 = tt; FOR(s, 1, 4) { queue <Truck>& Q = T[s]; while (SZ(Q) > 1) Q.pop(); if (!Q.empty()) // s != 2 { auto x = Q.front(); int v = Karol.v - x.v; long double where = x.front + x.v * t0; long double race = 1.0 * (where+1 - pos) / v; remax(tt, t0 + race); } } printf("%.15Lf\n", tt); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |