Problem p00034 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define PREP(i, m, n) for (int i = m; i < n; i++)
#define MREP(i, m, n) for (int i = m - 1; i >= n; i--)
using namespace std;

class train {
public:
  int v;
  double vRatio;
  double distance;
  int currentSta;

  train(int v = 0, double vRatio = 0.0, double distance = 0.0,
        int currentSta = 0)
      : v(v), vRatio(vRatio), distance(distance), currentSta(currentSta) {}

  void wantVRatio(int other) {
    vRatio = (double)v / ((double)v + (double)other);
  }

  void wantDistance(int railsum) { distance = (double)railsum * vRatio; }

private:
protected:
};

int main() {
  while (1) {
    map<int, int> rail;
    int railsum = 0;
    train up, down(0, 0.0, 0.0, 10);

    if (scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &rail[0], &rail[1],
              &rail[2], &rail[3], &rail[4], &rail[5], &rail[6], &rail[7],
              &rail[8], &rail[9], &up.v, &down.v) == EOF) {
      break;
    }

    PREP(i, 0, 10) { railsum += rail[i]; }

    up.wantVRatio(down.v);
    up.wantDistance(railsum);

    PREP(i, 0, 10) {
      if (up.distance <= rail[i]) {
        up.currentSta = i;
        break;
      }
      up.distance -= rail[i];
    }

    // この2の内訳は、インデックスが0~9までに対して問題の区間番号が1~10定義になっているので+1と
    // 仮に各電車の区間番号が5,7だった場合、すれ違うのは6なので5+1のための+1
    cout << up.currentSta + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define PREP(i, m, n) for (int i = m; i < n; i++)
#define MREP(i, m, n) for (int i = m - 1; i >= n; i--)
using namespace std;

class train {
public:
  int v;
  double vRatio;
  double distance;
  int currentSta;

  train(int v = 0, double vRatio = 0.0, double distance = 0.0,
        int currentSta = 0)
      : v(v), vRatio(vRatio), distance(distance), currentSta(currentSta) {}

  void wantVRatio(int other) {
    vRatio = (double)v / ((double)v + (double)other);
  }

  void wantDistance(int railsum) { distance = (double)railsum * vRatio; }

private:
protected:
};

int main() {
  while (1) {
    map<int, int> rail;
    int railsum = 0;
    train up, down(0, 0.0, 0.0, 10);

    scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &rail[0], &rail[1], &rail[2],
          &rail[3], &rail[4], &rail[5], &rail[6], &rail[7], &rail[8], &rail[9],
          &up.v, &down.v);

    PREP(i, 0, 10) { railsum += rail[i]; }

    up.wantVRatio(down.v);
    down.wantVRatio(up.v);

    while (1) {
      int uptmp = 0, downtmp = 0;
      up.wantDistance(railsum);
      down.wantDistance(railsum);

      PREP(i, up.currentSta, down.currentSta) {
        up.distance -= rail[i];
        if (up.distance < rail[i]) {
          uptmp = i;
          break;
        }
      }

      MREP(i, down.currentSta, up.currentSta) {
        down.distance -= rail[i];
        if (down.distance < rail[i]) {
          downtmp = i;
          break;
        }
      }

      up.currentSta = uptmp;
      down.currentSta = downtmp;

      if ((down.currentSta - up.currentSta) >= 3) {
        int sum = 0;
        PREP(i, up.currentSta, down.currentSta) { sum += rail[i]; }
        railsum = sum;
      } else if ((down.currentSta - up.currentSta) == 2) {
        // この2の内訳は、インデックスが0~9までに対して問題の区間番号が1~10定義になっているので+1と
        // 仮に各電車の区間番号が5,7だった場合、すれ違うのは6なので5+1のための+1
        cout << up.currentSta + 2 << endl;
        break;
      } else {
        // インデックスが0~9までに対して問題の区間番号が1~10定義になっているので+1
        cout << (up.currentSta > down.currentSta ? down.currentSta + 1
                                                 : up.currentSta + 1)
             << endl;
        break;
      }
    }
  }
  return 0;
}