#include #include #include using namespace std; const int MAXCARS = 100; const bool DEBUG = false; const double EPS = 0.0000001; struct car { double l; int s; } cars[MAXCARS]; double roundUp(double d) { int id = (int)(4*d + 1.0 - EPS); return id/4.0; } double roundDown(double d) { int id = (int)(4*d + EPS); return id/4.0; } double getNextTime(double time, double l, int s, int n) { double ans = 0.0; double intTime; for(int i=0; i 0.0) intTime = 0.25; } else if (cars[i].l >= l-1 && cars[i].s > s) { intTime = roundDown((l - cars[i].l)/(cars[i].s - s)); // time to intersect front of car if (intTime > 0.0) intTime = 0.25; } else if (cars[i].s > s) intTime = roundUp((l-1 - cars[i].l)/(cars[i].s - s)); // time to intersect back of car else intTime = roundUp((l - cars[i].l)/(cars[i].s - s)); // time to intersect front of car if (DEBUG) { cout << "car " << i << " intersect time = " << setprecision(10) << intTime << endl; } if (intTime > 0.0) { if (DEBUG) { cout << " updated to " << intTime << endl; } if (ans == 0.0 || intTime < ans) { ans = intTime; if (DEBUG) cout << " update next intersection time!" << endl; } } } if (DEBUG) { cout << "next intersection at time = " << ans+time << endl;; } return ans + time; } double calcNextLoc(double currTime, double nextTime, double currLoc, int speed) { return currLoc + (nextTime-currTime)*speed; } int main() { int s, d, icase = 0; double l; int n; cin >> l >> s >> d >> n; double time = 0.0; while (l != 0 || s != 0) { icase++; double newSpeed = s; int num = 1; for(int i=0; i> cars[i].l >> cars[i].s; if (cars[i].l >= l-1 && cars[i].l <= l) { num++; newSpeed += cars[i].s; } } if (num > 1) s = newSpeed/num; while (true) { double currtime = time; time += (d-l)/s; // time to get to destination at current speed double nextTime = getNextTime(currtime, l, s, n); // get next intersection time if (nextTime == currtime) { // cout << "This should never happen" << endl; break; } double nextLoc = calcNextLoc(currtime, nextTime, l, s); // check if intersection happens if (nextTime > time || nextLoc > d) { // after car reaches dest break; } newSpeed = s; num = 1; // determine which cars are next to Anna's car for(int i=0; i> l >> s >> d >> n; time = 0.0; } }