#include <algorithm> #include <iostream> #include <vector> #include <iomanip> // #include "../../simple-console-debug/debug.h" using namespace std; typedef long long ll; struct Zone{ int pos, len; }; struct PersistentTree { struct Node { Node *left, *right; int sum; }; int tree_size; vector<Node*>Roots; vector<Node*>all_nodes; Node* recursive_build_empty_tree(int n){ Node *node = new Node; all_nodes.push_back(node); node->sum = 0; if(n>1) { node->left = recursive_build_empty_tree(n/2); node->right = recursive_build_empty_tree(n/2); } else node->left = node->right = nullptr; return node; } PersistentTree(int n){ tree_size = 1; while (tree_size < n) tree_size *= 2; Roots.push_back(recursive_build_empty_tree(tree_size)); } ~PersistentTree(){ for (Node* node : all_nodes) delete node; } Node* _set(Node* node, int pos, int ts) { //pos is relative to left if (ts==1) { Node *new_node = new Node; all_nodes.push_back(new_node); new_node->sum = 1; new_node->left = new_node->right = nullptr; return new_node; } Node* new_node = new Node(*node); all_nodes.push_back(new_node); if (pos < ts/2) new_node->left = _set(node->left, pos, ts/2); else new_node->right = _set(node->right, pos-ts/2, ts/2); new_node->sum = new_node->left->sum + new_node->right->sum; return new_node; } int set(int pos) { Node* new_root = _set(Roots.back(), pos, tree_size); Roots.push_back(new_root); return Roots.size()-1; } //returned position is relative to left int _find_first_one_position(Node* node, int ts) { //assuming than node->sum > 0 if (ts == 1) return 0; if (node->left->sum > 0) return _find_first_one_position(node->left, ts/2); else return ts/2 + _find_first_one_position(node->right, ts/2); } //returned position and target_pos is relative to left int _find_one(Node* node, int target_pos, int ts) { //phase A - go down if (ts == 1) return -1; if (target_pos < ts/2) { int resp = _find_one(node->left, target_pos, ts/2); if (resp != -1) return resp; if (node->right->sum == 0) return -1; return _find_first_one_position(node->right, ts/2) + ts/2; } else { int resp = _find_one(node->right, target_pos-ts/2, ts/2); if (resp == -1) return -1; else return resp + ts/2; } } int find_first_one_greater_than(int revision, int init_pos) { // cerr << "revision = " << revision << " per " << Roots.size() << endl; return _find_one(Roots[revision], init_pos, tree_size); } }; ostream& operator<<(ostream& os, const Zone &z) { return os << "{" << z.pos << ", " << z.len << "}"; } struct MR { static int d1, d2, d3; ll g1, g2, g3; MR():g1(0),g2(0),g3(0){} MR(ll g1, ll g2, ll g3):g1(g1),g2(g2),g3(g3){} MR operator+(const MR& mr) const { return MR(g1+mr.g1, g2+mr.g2, g3+mr.g3); } MR operator-(const MR& mr) const { return MR(g1-mr.g1, g2-mr.g2, g3-mr.g3); } MR operator*(int x) const { return MR(g1*x, g2*x, g3*x); } bool operator<(const MR& r) const { return g1*d2*d3 + g2*d1*d3 + g3*d1*d2 < r.g1*d2*d3 + r.g2*d1*d3 + r.g3*d1*d2; } double get_value() const { return (double)g1/d1 + (double)g2/d2 + (double)g3/d3; } }; int MR::d1, MR::d2, MR::d3; //d1 = forward_velocity //d2 = my_velocity //d3 = backward_velocity pair<bool, MR> move_to_next_zone_forward(MR t0, Zone from, Zone via, bool infinite_via, Zone to) { int from_end = from.pos+from.len; int via_end = via.pos+via.len; //phase 1 -> go to end of own zone MR t1 = t0 + MR(0,from.len,0); //check whether possible if (MR(from_end-via.pos,0,0) < t1) return {false, MR()}; //phase 2 -> go to target in advance MR t2 = t1 + MR(0,to.pos-from_end,0); //phase 3 -> wait for zone arrival MR t3; if (infinite_via) t3 = t2; else t3 = max(MR(to.pos-via_end,0,0), t2); return {true, t3}; } pair<bool, MR> move_to_next_zone_backward(MR t0, Zone from, Zone via, bool infinite_via, Zone to) { // cerr << "BACKWARD t0=" << t0.get_value() << " from=" << from << " via=" << via << " to=" << to << endl; int from_end = from.pos+from.len; int via_end = via.pos+via.len; //phase 1 -> go to end of own zone MR t1 = t0 + MR(0,from.len,0); //phase 2 -> wait for zone arrival // cerr << "bw_vel=" << MR::d3 << " dist=" << via.pos-from_end << endl; MR t2 = max(MR(0,0,via.pos-from_end), t1); //phase 3 -> go to begin of next zone MR t3 = t2 + MR(0,to.pos-from_end,0); // cerr << "t0=" << t0.get_value() << " t1=" << t2.get_value() << " t2=" << t2.get_value() << " t3=" << t3.get_value() << endl; //verify result if (!infinite_via && MR(0,0,via_end-to.pos) < t3) return {false, MR()}; return {true, t3}; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int L, v0, v1, v2, v3; cin >> L >> v0 >> v1 >> v2 >> v3; //transform to zone notation vector<Zone>RoadZones[3]; //forward, central, backward for(int r = 0; r < 3; r++) { string str; cin >> str; str[0] = '.'; str += ".#"; for(int i = 1, zone_start = 0; i < (int)str.length() ;i++) if (zone_start != -1 && str[i] == '#') { RoadZones[r].push_back(Zone{.pos=zone_start, .len=i-zone_start-1}); zone_start = -1; } else if (zone_start == -1 && str[i]=='.') zone_start = i; } // cerr << "FORAWRD ZONES: " << deb::Container(RoadZones[0]) << endl; // cerr << "CENTER ZONES: " << deb::Container(RoadZones[1]) << endl; // cerr << "BACKWARD ZONES: " << deb::Container(RoadZones[2]) << endl; MR::d1 = v1-v2; MR::d2 = v0-v2; MR::d3 = -(v3-v2); //prepare structures vector<MR> ForwardInitPosition; for (Zone z : RoadZones[0]) ForwardInitPosition.push_back(MR(z.pos, 0, 0)); vector<MR> BackwardInitPosition; for (int i=0; i<(int)RoadZones[2].size(); i++) { Zone &z = RoadZones[2][i]; BackwardInitPosition.push_back(MR(0, 0, z.pos+z.len)); } vector<pair<ll,int>> BackwardLenSorted; //len*(v_my + v_bw), original id for (int i=0; i<(int)RoadZones[2].size(); i++) BackwardLenSorted.push_back({ll(RoadZones[2][i].len)*MR::d2, i}); sort(BackwardLenSorted.begin(), BackwardLenSorted.end()); PersistentTree tree(RoadZones[2].size()); for(int i=BackwardLenSorted.size()-1; i>=0; i--) { int revision = tree.set(BackwardLenSorted[i].second); BackwardLenSorted[i].second = revision; } // cerr << "BLS : " << deb::Container(BackwardLenSorted) << endl; //get distances to middle road zones int mz = RoadZones[1].size(); vector<MR>ArrTime(mz); for (int i=1; i<mz; i++) { bool inserted_anything = false; auto new_prop = [&](pair<bool,MR> t){ if (t.first && (!inserted_anything || t.second<ArrTime[i])) { ArrTime[i] = t.second; inserted_anything = true; } }; Zone from = RoadZones[1][i-1]; Zone to = RoadZones[1][i]; int from_end = from.pos+from.len; MR t_from_end = ArrTime[i-1] + MR(0,from.len,0); //forward road int fzi = upper_bound(ForwardInitPosition.begin(), ForwardInitPosition.end(), MR(from_end,0,0)-t_from_end) - ForwardInitPosition.begin() - 1; if (fzi>=0 && fzi<(int)RoadZones[0].size()) new_prop(move_to_next_zone_forward(ArrTime[i-1], from, RoadZones[0][fzi], fzi+1==RoadZones[0].size(), to)); //backward road new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2].back(), true, to)); //last backward manually int bzi = lower_bound(BackwardInitPosition.begin(), BackwardInitPosition.end(), MR(0,0,from_end)+t_from_end) - BackwardInitPosition.begin(); //find first intersecting if (bzi<(int)RoadZones[2].size()) { // cerr << "bzi = " << bzi << endl; new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2][bzi], bzi+1==RoadZones[2].size(), to)); //try first intersecting int delta = to.pos - from_end; ll size_bound = delta*(MR::d2+MR::d3); //calculate size bound // for (int j=bzi+1; j<(int)RoadZones[2].size(); j++) // if (RoadZones[2][j].len*MR::d2 >= size_bound) { // // new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2][j], j+1==RoadZones[2].size(), to)); // cerr << "brute found " << j << endl; // break; // } auto lenSortedIt = lower_bound(BackwardLenSorted.begin(), BackwardLenSorted.end(), pair<ll,int>{size_bound, -42}); //look for revision with all and only greater than size_bound if (lenSortedIt != BackwardLenSorted.end()) { int first_greater = tree.find_first_one_greater_than(lenSortedIt->second, bzi); //get id of first big enough zone later than bzi // cerr << "tree found " << first_greater << endl; if (first_greater != -1) new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2][first_greater], first_greater+1==(int)RoadZones[2].size(), to)); } } // cerr << "Arrival time of zone " << i << " = " << ArrTime[i].get_value() << endl; } //go final straight double t = ArrTime.back().get_value(); double time_to_beat_forward = max(0.0, (RoadZones[0].back().pos - RoadZones[1].back().pos + MR::d1*t)/(MR::d2-MR::d1)); double time_to_beat_backward = max(0.0, (RoadZones[2].back().pos - MR::d3*t - RoadZones[1].back().pos)/(MR::d3+MR::d2)); cout << fixed << setprecision(15) << t+max(time_to_beat_backward, time_to_beat_forward) << "\n"; 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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | #include <algorithm> #include <iostream> #include <vector> #include <iomanip> // #include "../../simple-console-debug/debug.h" using namespace std; typedef long long ll; struct Zone{ int pos, len; }; struct PersistentTree { struct Node { Node *left, *right; int sum; }; int tree_size; vector<Node*>Roots; vector<Node*>all_nodes; Node* recursive_build_empty_tree(int n){ Node *node = new Node; all_nodes.push_back(node); node->sum = 0; if(n>1) { node->left = recursive_build_empty_tree(n/2); node->right = recursive_build_empty_tree(n/2); } else node->left = node->right = nullptr; return node; } PersistentTree(int n){ tree_size = 1; while (tree_size < n) tree_size *= 2; Roots.push_back(recursive_build_empty_tree(tree_size)); } ~PersistentTree(){ for (Node* node : all_nodes) delete node; } Node* _set(Node* node, int pos, int ts) { //pos is relative to left if (ts==1) { Node *new_node = new Node; all_nodes.push_back(new_node); new_node->sum = 1; new_node->left = new_node->right = nullptr; return new_node; } Node* new_node = new Node(*node); all_nodes.push_back(new_node); if (pos < ts/2) new_node->left = _set(node->left, pos, ts/2); else new_node->right = _set(node->right, pos-ts/2, ts/2); new_node->sum = new_node->left->sum + new_node->right->sum; return new_node; } int set(int pos) { Node* new_root = _set(Roots.back(), pos, tree_size); Roots.push_back(new_root); return Roots.size()-1; } //returned position is relative to left int _find_first_one_position(Node* node, int ts) { //assuming than node->sum > 0 if (ts == 1) return 0; if (node->left->sum > 0) return _find_first_one_position(node->left, ts/2); else return ts/2 + _find_first_one_position(node->right, ts/2); } //returned position and target_pos is relative to left int _find_one(Node* node, int target_pos, int ts) { //phase A - go down if (ts == 1) return -1; if (target_pos < ts/2) { int resp = _find_one(node->left, target_pos, ts/2); if (resp != -1) return resp; if (node->right->sum == 0) return -1; return _find_first_one_position(node->right, ts/2) + ts/2; } else { int resp = _find_one(node->right, target_pos-ts/2, ts/2); if (resp == -1) return -1; else return resp + ts/2; } } int find_first_one_greater_than(int revision, int init_pos) { // cerr << "revision = " << revision << " per " << Roots.size() << endl; return _find_one(Roots[revision], init_pos, tree_size); } }; ostream& operator<<(ostream& os, const Zone &z) { return os << "{" << z.pos << ", " << z.len << "}"; } struct MR { static int d1, d2, d3; ll g1, g2, g3; MR():g1(0),g2(0),g3(0){} MR(ll g1, ll g2, ll g3):g1(g1),g2(g2),g3(g3){} MR operator+(const MR& mr) const { return MR(g1+mr.g1, g2+mr.g2, g3+mr.g3); } MR operator-(const MR& mr) const { return MR(g1-mr.g1, g2-mr.g2, g3-mr.g3); } MR operator*(int x) const { return MR(g1*x, g2*x, g3*x); } bool operator<(const MR& r) const { return g1*d2*d3 + g2*d1*d3 + g3*d1*d2 < r.g1*d2*d3 + r.g2*d1*d3 + r.g3*d1*d2; } double get_value() const { return (double)g1/d1 + (double)g2/d2 + (double)g3/d3; } }; int MR::d1, MR::d2, MR::d3; //d1 = forward_velocity //d2 = my_velocity //d3 = backward_velocity pair<bool, MR> move_to_next_zone_forward(MR t0, Zone from, Zone via, bool infinite_via, Zone to) { int from_end = from.pos+from.len; int via_end = via.pos+via.len; //phase 1 -> go to end of own zone MR t1 = t0 + MR(0,from.len,0); //check whether possible if (MR(from_end-via.pos,0,0) < t1) return {false, MR()}; //phase 2 -> go to target in advance MR t2 = t1 + MR(0,to.pos-from_end,0); //phase 3 -> wait for zone arrival MR t3; if (infinite_via) t3 = t2; else t3 = max(MR(to.pos-via_end,0,0), t2); return {true, t3}; } pair<bool, MR> move_to_next_zone_backward(MR t0, Zone from, Zone via, bool infinite_via, Zone to) { // cerr << "BACKWARD t0=" << t0.get_value() << " from=" << from << " via=" << via << " to=" << to << endl; int from_end = from.pos+from.len; int via_end = via.pos+via.len; //phase 1 -> go to end of own zone MR t1 = t0 + MR(0,from.len,0); //phase 2 -> wait for zone arrival // cerr << "bw_vel=" << MR::d3 << " dist=" << via.pos-from_end << endl; MR t2 = max(MR(0,0,via.pos-from_end), t1); //phase 3 -> go to begin of next zone MR t3 = t2 + MR(0,to.pos-from_end,0); // cerr << "t0=" << t0.get_value() << " t1=" << t2.get_value() << " t2=" << t2.get_value() << " t3=" << t3.get_value() << endl; //verify result if (!infinite_via && MR(0,0,via_end-to.pos) < t3) return {false, MR()}; return {true, t3}; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int L, v0, v1, v2, v3; cin >> L >> v0 >> v1 >> v2 >> v3; //transform to zone notation vector<Zone>RoadZones[3]; //forward, central, backward for(int r = 0; r < 3; r++) { string str; cin >> str; str[0] = '.'; str += ".#"; for(int i = 1, zone_start = 0; i < (int)str.length() ;i++) if (zone_start != -1 && str[i] == '#') { RoadZones[r].push_back(Zone{.pos=zone_start, .len=i-zone_start-1}); zone_start = -1; } else if (zone_start == -1 && str[i]=='.') zone_start = i; } // cerr << "FORAWRD ZONES: " << deb::Container(RoadZones[0]) << endl; // cerr << "CENTER ZONES: " << deb::Container(RoadZones[1]) << endl; // cerr << "BACKWARD ZONES: " << deb::Container(RoadZones[2]) << endl; MR::d1 = v1-v2; MR::d2 = v0-v2; MR::d3 = -(v3-v2); //prepare structures vector<MR> ForwardInitPosition; for (Zone z : RoadZones[0]) ForwardInitPosition.push_back(MR(z.pos, 0, 0)); vector<MR> BackwardInitPosition; for (int i=0; i<(int)RoadZones[2].size(); i++) { Zone &z = RoadZones[2][i]; BackwardInitPosition.push_back(MR(0, 0, z.pos+z.len)); } vector<pair<ll,int>> BackwardLenSorted; //len*(v_my + v_bw), original id for (int i=0; i<(int)RoadZones[2].size(); i++) BackwardLenSorted.push_back({ll(RoadZones[2][i].len)*MR::d2, i}); sort(BackwardLenSorted.begin(), BackwardLenSorted.end()); PersistentTree tree(RoadZones[2].size()); for(int i=BackwardLenSorted.size()-1; i>=0; i--) { int revision = tree.set(BackwardLenSorted[i].second); BackwardLenSorted[i].second = revision; } // cerr << "BLS : " << deb::Container(BackwardLenSorted) << endl; //get distances to middle road zones int mz = RoadZones[1].size(); vector<MR>ArrTime(mz); for (int i=1; i<mz; i++) { bool inserted_anything = false; auto new_prop = [&](pair<bool,MR> t){ if (t.first && (!inserted_anything || t.second<ArrTime[i])) { ArrTime[i] = t.second; inserted_anything = true; } }; Zone from = RoadZones[1][i-1]; Zone to = RoadZones[1][i]; int from_end = from.pos+from.len; MR t_from_end = ArrTime[i-1] + MR(0,from.len,0); //forward road int fzi = upper_bound(ForwardInitPosition.begin(), ForwardInitPosition.end(), MR(from_end,0,0)-t_from_end) - ForwardInitPosition.begin() - 1; if (fzi>=0 && fzi<(int)RoadZones[0].size()) new_prop(move_to_next_zone_forward(ArrTime[i-1], from, RoadZones[0][fzi], fzi+1==RoadZones[0].size(), to)); //backward road new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2].back(), true, to)); //last backward manually int bzi = lower_bound(BackwardInitPosition.begin(), BackwardInitPosition.end(), MR(0,0,from_end)+t_from_end) - BackwardInitPosition.begin(); //find first intersecting if (bzi<(int)RoadZones[2].size()) { // cerr << "bzi = " << bzi << endl; new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2][bzi], bzi+1==RoadZones[2].size(), to)); //try first intersecting int delta = to.pos - from_end; ll size_bound = delta*(MR::d2+MR::d3); //calculate size bound // for (int j=bzi+1; j<(int)RoadZones[2].size(); j++) // if (RoadZones[2][j].len*MR::d2 >= size_bound) { // // new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2][j], j+1==RoadZones[2].size(), to)); // cerr << "brute found " << j << endl; // break; // } auto lenSortedIt = lower_bound(BackwardLenSorted.begin(), BackwardLenSorted.end(), pair<ll,int>{size_bound, -42}); //look for revision with all and only greater than size_bound if (lenSortedIt != BackwardLenSorted.end()) { int first_greater = tree.find_first_one_greater_than(lenSortedIt->second, bzi); //get id of first big enough zone later than bzi // cerr << "tree found " << first_greater << endl; if (first_greater != -1) new_prop(move_to_next_zone_backward(ArrTime[i-1], from, RoadZones[2][first_greater], first_greater+1==(int)RoadZones[2].size(), to)); } } // cerr << "Arrival time of zone " << i << " = " << ArrTime[i].get_value() << endl; } //go final straight double t = ArrTime.back().get_value(); double time_to_beat_forward = max(0.0, (RoadZones[0].back().pos - RoadZones[1].back().pos + MR::d1*t)/(MR::d2-MR::d1)); double time_to_beat_backward = max(0.0, (RoadZones[2].back().pos - MR::d3*t - RoadZones[1].back().pos)/(MR::d3+MR::d2)); cout << fixed << setprecision(15) << t+max(time_to_beat_backward, time_to_beat_forward) << "\n"; return 0; } |