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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using namespace rel_ops;
struct rat {
  int a, b;
  rat(int a, int b) : a(a), b(b) {}
};
inline int gcd(int x, int y) {
  if (y == 0)
    return abs(x);
  return gcd(y, x % y);
}
inline rat normz(rat x) {
  int g = gcd(x.a, x.b);
  // if(x.b<0) x.a*=-1,x.b*=-1;
  return rat(x.a / g, x.b / g);
}
inline rat operator+(const rat &x, const rat &y) {
  return normz(rat(x.a * y.b + x.b * y.a, x.b * y.b));
}
inline rat operator-(const rat &x, const rat &y) {
  return normz(rat(x.a * y.b - x.b * y.a, x.b * y.b));
}
inline bool operator<(const rat &x, const rat &y) {
  return x.a * y.b < x.b * y.a;
}
inline bool operator==(const rat &x, const rat &y) {
  return x.a * y.b == x.b * y.a;
}

typedef vector<int> Vi;
int main() {
  while (true) {
    int N, t[50], c[50];
    cin >> N;
    if (N == 0)
      break;
    rep(i, N) cin >> t[i] >> c[i];
    map<rat, Vi> mp; // pos->id
    Vi fst;
    rep(i, N) fst.pb(i);
    mp[rat(0, 1)] = fst;
    int tm = 0;
    while (true) {
      map<rat, Vi> nmp;
      int T = 1e9;
      for (auto it : mp) {
        bool slow = 1;
        rat r = it.fs;
        Vi vc = it.sc;
        if (r == rat(0, 1) || r == rat(1, 1))
          slow = 0;
        if (slow) {
          int mx = 0;
          for (int v : vc)
            chmax(mx, t[v]);
          rat s(1, 1);
          if (r < rat(1, 1))
            s = rat(1, 1) - r;
          else
            s = rat(2, 1) - r;
          chmin(T, s.a * mx / s.b);
        } else {
          for (int v : vc) {
            rat s(1, 1);
            if (r < rat(1, 1))
              s = rat(1, 1) - r;
            else
              s = rat(2, 1) - r;
            chmin(T, s.a * t[v] / s.b);
          }
        }
      }
      rat mn(2, 1);
      for (auto it = mp.rbegin(); it != mp.rend(); it++) {
        bool slow = 1;
        rat r = it->fs;
        Vi vc = it->sc;
        if (r == rat(0, 1) || r == rat(1, 1))
          slow = 0;
        if (slow) {
          int mxt = 0;
          for (int v : vc)
            chmax(mxt, t[v]);
          rat nr = min(r + rat(T, mxt), mn);
          chmin(mn, nr);
          nmp[nr].insert(nmp[nr].end(), vc.begin(), vc.end());
        } else {
          for (int v : vc) {
            rat nr = min(r + rat(T, t[v]), mn);
            nmp[nr].pb(v);
          }
        }
      }
      for (int v : nmp[rat(2, 1)]) {
        c[v]--;
        if (c[v])
          nmp[rat(0, 1)].pb(v);
      }
      nmp.erase(rat(2, 1));
      mp = nmp;
      tm += T;
      if (mp.empty()) {
        cout << tm << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(c) c.begin(), c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;
using namespace rel_ops;
struct rat {
  int a, b;
  rat(int a, int b) : a(a), b(b) {}
};
inline int gcd(int x, int y) {
  if (y == 0)
    return abs(x);
  return gcd(y, x % y);
}
inline rat normz(rat x) {
  int g = gcd(x.a, x.b);
  // if(x.b<0) x.a*=-1,x.b*=-1;
  return rat(x.a / g, x.b / g);
}
inline rat operator+(const rat &x, const rat &y) {
  return normz(rat(x.a * y.b + x.b * y.a, x.b * y.b));
}
inline bool operator<(const rat &x, const rat &y) {
  return x.a * y.b < x.b * y.a;
}
inline bool operator==(const rat &x, const rat &y) {
  return x.a * y.b == x.b * y.a;
}

typedef vector<int> Vi;
int main() {
  while (true) {
    int N, t[50], c[50];
    cin >> N;
    if (N == 0)
      break;
    rep(i, N) cin >> t[i] >> c[i];
    map<rat, Vi> mp; // pos->id
    Vi fst;
    rep(i, N) fst.pb(i);
    mp[rat(0, 1)] = fst;
    rep(tm, 150000) {
      map<rat, Vi> nmp;
      rat mn(2, 1);
      for (auto it = mp.rbegin(); it != mp.rend(); it++) {
        bool slow = 1;
        rat r = it->fs;
        Vi vc = it->sc;
        if (r == rat(0, 1) || r == rat(1, 1))
          slow = 0;
        if (slow) {
          int mxt = 0;
          for (int v : vc)
            chmax(mxt, t[v]);
          rat nr = min(r + rat(1, mxt), mn);
          chmin(mn, nr);
          nmp[nr].insert(nmp[nr].end(), vc.begin(), vc.end());
        } else {
          for (int v : vc) {
            rat nr = min(r + rat(1, t[v]), mn);
            nmp[nr].pb(v);
          }
        }
      }
      for (int v : nmp[rat(2, 1)]) {
        c[v]--;
        if (c[v])
          nmp[rat(0, 1)].pb(v);
      }
      nmp.erase(rat(2, 1));
      mp = nmp;
      if (mp.empty()) {
        cout << tm + 1 << endl;
        break;
      }
    }
  }
}