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

#include <algorithm>
#include <list>
#include <stdio.h>
#include <utility>
using namespace std;

typedef list<int> OuterList;
struct Doll {
  int outerCount; // ±êðüêçêél`ÌÂ
  int h, r;       // å«³
  OuterList
      outerList; // ±êðüêçêél`ÌQÆæÌCfbNX
};
const int N = 200;
// ärÖÌ¹¢Å±±É@ÝvÔá¦½H
Doll doll[N];

// åÌå«¢ÉÀÑÖ¦éæ¤
bool SortDoll(const Doll &lhv, const Doll &rhv) {
  return (lhv.h + lhv.r) > (rhv.h + rhv.r);
}
// êÔO¤Ìl`ª½¢ÌðÔ·
// max_elementÉn·àÌÍAoperator<ãíèÌàÌ
bool CmpOuterDoll(int lhv, int rhv) {
  return doll[lhv].outerCount < doll[rhv].outerCount;
}

int main() {
  int n, m, i, j, dollCount;
  const int INVALID_VALUE = -1;

  while (scanf("%d", &n), n) {
    for (i = 0; i < n; ++i)
      scanf("%d%d", &doll[i].h, &doll[i].r);
    scanf("%d", &m);
    for (i = 0; i < m; ++i)
      scanf("%d%d", &doll[i + n].h, &doll[i + n].r);

    dollCount = n + m;
    // åÌå«¢ÉÀÑÖ¦é
    sort(doll, doll + dollCount, SortDoll);

    //  él`ðàïÅ«él`Ìñ
    for (i = 0; i < dollCount; ++i) {
      doll[i].outerCount =
          INVALID_VALUE;         // Ü¾mèµÄ¢Ü¹ñæ
      doll[i].outerList.clear(); // Á¦È³¢æ
      for (j = 0; j < dollCount; ++j) {
        // üèÜ·H
        if (doll[i].h < doll[j].h && doll[i].r < doll[j].r) {
          doll[i].outerList.push_back(j);
        }
      }
    }

    int finishCount = 0;
    int maxDoll = 0;
    // Sl`ªðI¦éÜÅ[v
    while (finishCount < dollCount) {
      for (i = 0; i < dollCount; ++i) {
        // Ü¾»Ìl`ÌªIíÁÄ¢È¢Èç
        if (doll[i].outerCount == INVALID_VALUE) {
          bool f =
              true; // SÄÌO¤ÌzªðI¦Ä¢é©
          for (OuterList::iterator ite = doll[i].outerList.begin();
               ite != doll[i].outerList.end(); ++ite) {
            if (doll[*ite].outerCount == INVALID_VALUE) {
              f = false;
              break;
            }
          }
          // SÄIíÁÄ¢éÈçA él`ÍA»Ìl`ðÍ¦él`ÌÂ+1@ÂÉÈèÜ·
          if (f) {
            if (doll[i].outerList.empty()) {
              // ½à»êðÍ¤±ÆªoÈ¢l`ÈÌÅ·
              doll[i].outerCount = 1;
            } else {
              // ±êð©ÄAY¦ðÇµ½±Æðã÷·éÌÅµ½
              doll[i].outerCount =
                  doll[*max_element(doll[i].outerList.begin(),
                                    doll[i].outerList.end(), CmpOuterDoll)]
                      .outerCount +
                  1;
            }
            maxDoll = max(maxDoll, doll[i].outerCount);
            finishCount++; // Ü½êÂmèµÜµ½
          }
        }
      }
    }

    printf("%d\\n", maxDoll);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <list>
#include <stdio.h>
#include <utility>
using namespace std;

typedef list<int> OuterList;
struct Doll {
  int outerCount;
  int h, r;
  OuterList outerList;
};
const int N = 200;
Doll doll[N];

bool SortDoll(const Doll &lhv, const Doll &rhv) {
  return (lhv.h + lhv.r) > (rhv.h + rhv.r);
}
bool CmpOuterDoll(int lhv, int rhv) {
  return doll[lhv].outerCount < doll[rhv].outerCount;
}

int main() {
  int n, m, i, j, dollCount;
  const int INVALID_VALUE = -1;

  while (scanf("%d", &n), n) {
    for (i = 0; i < n; ++i)
      scanf("%d%d", &doll[i].h, &doll[i].r);
    scanf("%d", &m);
    for (i = 0; i < m; ++i)
      scanf("%d%d", &doll[i + n].h, &doll[i + n].r);

    dollCount = n + m;
    sort(doll, doll + dollCount, SortDoll);

    for (i = 0; i < dollCount; ++i) {
      doll[i].outerCount = INVALID_VALUE;
      doll[i].outerList.clear();
      for (j = 0; j < dollCount; ++j) {
        if (doll[i].h < doll[j].h && doll[i].r < doll[j].r) {
          doll[i].outerList.push_back(j);
        }
      }
    }

    int finishCount = 0;
    int maxDoll = 0;
    while (finishCount < dollCount) {
      for (i = 0; i < dollCount; ++i) {
        if (doll[i].outerCount == INVALID_VALUE) {
          bool f = true;
          for (OuterList::iterator ite = doll[i].outerList.begin();
               ite != doll[i].outerList.end(); ++ite) {
            if (doll[*ite].outerCount == INVALID_VALUE) {
              f = false;
              break;
            }
          }
          if (f) {
            if (doll[i].outerList.empty()) {
              doll[i].outerCount = 1;
            } else {
              doll[i].outerCount =
                  doll[*max_element(doll[i].outerList.begin(),
                                    doll[i].outerList.end(), CmpOuterDoll)]
                      .outerCount +
                  1;
            }
            maxDoll = max(maxDoll, doll[i].outerCount);
          }
        }
      }
    }

    printf("%d\\n", maxDoll);
  }
  return 0;
}