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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef struct {
  int width;
  int height;
} CAKE;

int main() {
  int n, w, d;

  while (cin >> n >> w >> d, n || w || d) {

    vector<CAKE> cake;
    CAKE tmp = {w, d};
    cake.push_back(tmp);
    int p, s;
    while (n--) {
      cin >> p >> s;
      p--;

      int cnt = 0;
      while (true) {
        if (s - cake[p].width <= 0)
          break;
        s -= cake[p].width;
        cnt++;

        if (s - cake[p].height <= 0)
          break;
        s -= cake[p].height;
        cnt++;
      }

      cnt %= 4;
      // cout << "cnt" << cnt << endl;
      pair<CAKE, CAKE> P;
      if (cnt % 2 == 0) {

        CAKE tmp = {s, cake[p].height};
        P.first = tmp;
        tmp.width = cake[p].width - s;
        P.second = tmp;

      } else {
        CAKE tmp = {cake[p].width, s};
        P.first = tmp;
        tmp.height = cake[p].height - s;
        P.second = tmp;
      }

      if (P.first.width * P.first.height > P.second.width * P.second.height) {
        cake.push_back(P.second);
        cake.push_back(P.first);
        cake.erase(cake.begin() + p);
      } else {
        cake.push_back(P.first);
        cake.push_back(P.second);
        cake.erase(cake.begin() + p);
      }
    }

    for (int i = 0; i < cake.size(); i++) {
      for (int j = 0; j < cake.size() - 1; j++) {
        if (cake[j].width * cake[j].height >
            cake[j + 1].width * cake[j + 1].height)
          swap(cake[j], cake[j + 1]);
      }
    }

    for (int i = 0; i < cake.size() - 1; i++) {
      cout << cake[i].width * cake[i].height << " ";
    }
    cout << cake[cake.size() - 1].width * cake[cake.size() - 1].height << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef struct {
  int width;
  int height;
} CAKE;

int main() {
  int n, w, d;

  while (cin >> n >> w >> d, n || w || d) {

    vector<CAKE> cake;
    CAKE tmp = {w, d};
    cake.push_back(tmp);
    int p, s;
    while (n--) {
      cin >> p >> s;
      p--;

      int cnt = 0;
      while (true) {
        if (s - cake[p].width <= 0)
          break;
        s -= cake[p].width;
        cnt++;

        if (s - cake[p].height <= 0)
          break;
        s -= cake[p].height;
        cnt++;
      }

      cnt %= 4;
      cout << "cnt" << cnt << endl;
      pair<CAKE, CAKE> P;
      if (cnt % 2 == 0) {

        CAKE tmp = {s, cake[p].height};
        P.first = tmp;
        tmp.width = cake[p].width - s;
        P.second = tmp;

      } else {
        CAKE tmp = {cake[p].width, s};
        P.first = tmp;
        tmp.height = cake[p].height - s;
        P.second = tmp;
      }

      // if (  P.first.width * P.first.height > P.second.width * P.second.height
      // ) {
      //    cake.push_back(P.second);
      //     cake.push_back(P.first);
      //  cake.erase(cake.begin()+p);
      //    }else{
      cake.push_back(P.first);
      cake.push_back(P.second);
      cake.erase(cake.begin() + p);
      //    }
    }

    for (int i = 0; i < cake.size(); i++) {
      for (int j = 0; j < cake.size() - 1; j++) {
        if (cake[j].width * cake[j].height >
            cake[j + 1].width * cake[j + 1].height)
          swap(cake[j], cake[j + 1]);
      }
    }

    for (int i = 0; i < cake.size() - 1; i++) {
      cout << cake[i].width * cake[i].height << " ";
    }
    cout << cake[cake.size() - 1].width * cake[cake.size() - 1].height << endl;
  }

  return 0;
}