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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)

struct swimmer {
  double pos;
  // double prepos;
  int t;
  int rest_loop;
  int lane;
};

swimmer ss[100];
int order[100];

bool finish(int &n) {
  int cnt = 0;
  rep(i, n) if (ss[i].rest_loop) { cnt++; }
  if (cnt == 0)
    return true;
  n = cnt;
  return false;
}

void move(int n) {
  /*
  cout << "************************" << endl;
  cout << "begin" << endl;
  rep(i, n)cout << ss[i].pos << " " << ss[i].rest_loop << " " << ss[i].lane <<
  endl; cout << "***********************" << endl;
  */
  double m[2] = {1.0, 1.0};
  rep(i, n) if (ss[i].rest_loop) {
    // ss[i].prepos = ss[i].pos;
    ss[i].pos += 1.0 / ss[i].t;
  }
  rep(i, n) {
    if (ss[i].rest_loop == 0)
      continue;
    if (ss[i].pos > m[ss[i].lane]) {
      ss[i].pos = m[ss[i].lane];
    } else {
      m[ss[i].lane] = ss[i].pos;
    }
  }

  rep(i, n) {
    if (ss[i].rest_loop == 0)
      continue;
    if (abs(ss[i].pos - 1.0) < EPS) {
      ss[i].pos = 0.0;
      if (ss[i].lane == 0) {
        ss[i].lane = 1;
      } else {
        ss[i].lane = 0;
        ss[i].rest_loop--;
      }
    }
  }
  /*
  cout << "************************" << endl;
  cout << "end" << endl;
  rep(i, n)cout << ss[i].pos << " " << ss[i].rest_loop << " " << ss[i].lane <<
  endl; cout << "***********************" << endl;
  */
}

bool comp(const swimmer &s1, const swimmer &s2) {
  if (s1.rest_loop == 0 || s2.rest_loop == 0)
    return s1.rest_loop > s2.rest_loop;
  else if (s1.lane != s2.lane)
    return s1.lane < s2.lane;
  else if (abs(s1.pos - s2.pos) < EPS) {
    if (s1.pos < EPS && s2.pos < EPS)
      return s1.t < s2.t;
    else
      return s1.t > s2.t;
  } else {
    return s1.pos > s2.pos;
  }
}

int main() {
  int n;
  while (cin >> n && n) {
    rep(i, n) {
      cin >> ss[i].t >> ss[i].rest_loop;
      ss[i].lane = 0;
      ss[i].pos = 0.0;
    }

    int cnt = 0;
    sort(ss, ss + n, comp);
    while (true) {
      if (finish(n)) {
        cout << cnt << endl;
        break;
      }
      cnt++;
      move(n);
      sort(ss, ss + n, comp);
      assert(cnt < 800000);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)

struct swimmer {
  double pos;
  // double prepos;
  int t;
  int rest_loop;
  int lane;
};

swimmer ss[100];
int order[100];

bool finish(int n) {
  rep(i, n) if (ss[i].rest_loop) return false;
  return true;
}

void move(int n) {
  /*
  cout << "************************" << endl;
  cout << "begin" << endl;
  rep(i, n)cout << ss[i].pos << " " << ss[i].rest_loop << " " << ss[i].lane <<
  endl; cout << "***********************" << endl;
  */
  double m[2] = {1.0, 1.0};
  rep(i, n) if (ss[i].rest_loop) {
    // ss[i].prepos = ss[i].pos;
    ss[i].pos += 1.0 / ss[i].t;
  }
  rep(i, n) {
    if (ss[i].rest_loop == 0)
      continue;
    if (ss[i].pos > m[ss[i].lane]) {
      ss[i].pos = m[ss[i].lane];
    } else {
      m[ss[i].lane] = ss[i].pos;
    }
  }

  rep(i, n) {
    if (ss[i].rest_loop == 0)
      continue;
    if (abs(ss[i].pos - 1.0) < EPS) {
      ss[i].pos = 0.0;
      if (ss[i].lane == 0) {
        ss[i].lane = 1;
      } else {
        ss[i].lane = 0;
        ss[i].rest_loop--;
      }
    }
  }
  /*
  cout << "************************" << endl;
  cout << "end" << endl;
  rep(i, n)cout << ss[i].pos << " " << ss[i].rest_loop << " " << ss[i].lane <<
  endl; cout << "***********************" << endl;
  */
}

bool comp(const swimmer &s1, const swimmer &s2) {
  if (s1.lane != s2.lane)
    return s1.lane < s2.lane;
  else if (s1.rest_loop == 0 || s2.rest_loop == 0)
    return s1.rest_loop < s2.rest_loop;
  else if (abs(s1.pos - s2.pos) < EPS) {
    if (s1.pos < EPS && s2.pos < EPS)
      return s1.t < s2.t;
    else
      return s1.t > s2.t;
  } else {
    return s1.pos > s2.pos;
  }
}

int main() {
  int n;
  while (cin >> n && n) {
    rep(i, n) {
      cin >> ss[i].t >> ss[i].rest_loop;
      ss[i].lane = 0;
      ss[i].pos = 0.0;
    }

    int cnt = 0;
    sort(ss, ss + n, comp);
    while (true) {
      if (finish(n)) {
        cout << cnt << endl;
        break;
      }
      cnt++;
      move(n);
      sort(ss, ss + n, comp);
      assert(cnt < 800000);
    }
  }
  return 0;
}