alexjercan/bugnet huggingface dataset:
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> MAT;
#define h first
#define r second
#define rep(i, n) for (int i = 0; i < n; i++)
int dp[1000][1000];
MAT info[200];
int n, m;

int dfs(int a, int b) {
  if (~dp[a][b])
    return dp[a][b];

  int hoge = 0;
  rep(i, n) {
    if (info[i].first < a && info[i].second < b) {
      hoge = max(hoge, dfs(info[i].first, info[i].second) + 1);
    }
  }
  return dp[a][b] = hoge;
}
int main() {
  while (cin >> n, n) {
    rep(i, 1000) rep(j, 1000) dp[i][j] = -1;

    int ret = 0;
    rep(i, n) cin >> info[i].h >> info[i].r;
    cin >> m;
    rep(i, m) cin >> info[n + i].h >> info[n + i].r;

    n = n + m;

    rep(i, n) { ret = max(ret, dfs(info[i].first, info[i].second) + 1); }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> MAT;
#define h first
#define r second
#define rep(i, n) for (int i = 0; i < n; i++)
int dp[1000][1000];
MAT info[200];
int n, m;

int dfs(int a, int b) {
  if (~dp[a][b])
    return dp[a][b];

  int hoge = 0;
  rep(i, n) {
    if (info[i].first < a && info[i].second < b) {
      hoge = max(hoge, dfs(info[i].first, info[i].second) + 1);
    }
  }
  return hoge;
}
int main() {
  while (cin >> n, n) {
    rep(i, 1000) rep(j, 1000) dp[i][j] = -1;

    int ret = 0;
    rep(i, n) cin >> info[i].h >> info[i].r;
    cin >> m;
    rep(i, m) cin >> info[n + i].h >> info[n + i].r;

    n = n + m;

    rep(i, n) { ret = max(ret, dfs(info[i].first, info[i].second) + 1); }
    cout << ret << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int ichirou, zirou, ningyo[1010][1010], dp[1010][1010];

int main() {
  while (true) {
    for (int i = 0; i < 1010; i++) {
      for (int j = 0; j < 1010; j++) {
        ningyo[i][j] = dp[i][j] = 0;
      }
    }

    cin >> ichirou;
    if (ichirou == 0) {
      break;
    }
    for (int i = 0; i < ichirou; i++) {
      int a, b;
      cin >> a;
      cin >> b;
      ningyo[a][b] = 1;
    }
    cin >> zirou;
    for (int i = 0; i < zirou; i++) {
      int a, b;
      cin >> a;
      cin >> b;
      ningyo[a][b] = 1;
    }

    int a, b;
    for (b = 1; b < 1001; b++) {
      for (a = 1; a < 1001; a++) {
        if (ningyo[a][b] == 1) {
          dp[a][b] = dp[a - 1][b - 1] + 1;
        } else {
          dp[a][b] = max(dp[a - 1][b], dp[a][b - 1]);
        }
      }
    }
    cout << dp[1000][1000] << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int loop, ichirou, zirou, ningyo[110][110], dp[110][110];

int main() {
  while (true) {
    for (int i = 0; i < 101; i++) {
      for (int j = 0; j < 101; j++) {
        ningyo[i][j] = dp[i][j] = 0;
      }
    }

    cin >> ichirou;
    if (ichirou == 0) {
      break;
    }
    for (int i = 0; i < ichirou; i++) {
      int a, b;
      cin >> a;
      cin >> b;
      ningyo[a][b] = 1;
    }
    cin >> zirou;
    for (int i = 0; i < zirou; i++) {
      int a, b;
      cin >> a;
      cin >> b;
      ningyo[a][b] = 1;
    }

    int a, b;
    for (b = 1; b < 101; b++) {
      for (a = 1; a < 101; a++) {
        if (ningyo[a][b] == 1) {
          dp[a][b] = dp[a - 1][b - 1] + 1;
        } else {
          dp[a][b] = max(dp[a - 1][b], dp[a][b - 1]);
        }
      }
    }
    cout << dp[100][100] << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded


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

struct Doll {
  int h, r;
  Doll(int a, int b) {
    h = a;
    r = b;
  };
};

int n;
int h, r;
int m;
vector<Doll> doll;
int memo[201] = {0};

bool comp(Doll a, Doll b) { return a.h < b.h && a.r < b.r; };

int rec(int id) {
  if (memo[id] != 0)
    return memo[id];

  int ans = 0;
  for (int i = 0; i < n + m; i++) {
    if (comp(doll[i], doll[id]))
      ans = max(rec(i) + 1, ans);
  }

  return memo[id] = ans;
}

int main() {
  while (cin >> n, n) {
    doll.clear();
    for (int i = 0; i < 200; i++)
      memo[i] = 0;

    for (int i = 0; i < n; i++) {
      cin >> h >> r;
      doll.push_back(Doll(h, r));
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> h >> r;
      doll.push_back(Doll(h, r));
    }

    doll.push_back(Doll(1001, 1001));

    cout << rec(n + m) << endl;
  }
}

Error: TLE

Fixed submission:


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

struct Doll {
  int h, r;
  Doll(int a, int b) {
    h = a;
    r = b;
  };
};

int n;
int h, r;
int m;
vector<Doll> doll;
int memo[201] = {0};

bool comp(Doll a, Doll b) { return a.h < b.h && a.r < b.r; };

int rec(int id) {
  int ans = 0;
  for (int i = 0; i < n + m; i++) {
    if (comp(doll[i], doll[id]))
      ans = max(rec(i) + 1, ans);
  }

  return ans;
}

int main() {
  while (cin >> n, n) {
    doll.clear();
    for (int i = 0; i < 200; i++)
      memo[i] = 0;

    for (int i = 0; i < n; i++) {
      cin >> h >> r;
      doll.push_back(Doll(h, r));
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> h >> r;
      doll.push_back(Doll(h, r));
    }

    doll.push_back(Doll(1001, 1001));

    cout << rec(n + m) << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

// Solution for aoj:0157 Russian Dolls
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;

int main() {
  int n, m, a, b, ans;
  P doll[201];
  int dp[201];
  while (cin >> n && n) {
    fill(dp, dp + 201, 0);
    ans = 0;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++) {
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    for (int i = 0; i < n + m; i++) {
      for (int j = 0; j < i; j++) {
        if (doll[i].first > doll[j].first && doll[i].second > doll[j].second) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
        ans = max(ans, dp[i]);
      }
    }
    cout << ans + 1 << endl;
  }
}

Error: 0

Fixed submission:

// Solution for aoj:0157 Russian Dolls
#include <algorithm>
#include <iostream>
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int n, m, ans, p, s, s2, s3;
pair<int, int> doll[201];
int dp[2][201], dp2[201];
int d[2][1000];
int main() {
  while (cin >> n) {
    if (!n)
      return 0;
    fill(dp[0], dp[0] + 1000, INF);
    fill(dp[1], dp[1] + 1000, INF);
    fill(dp2, dp2 + 201, INF);
    for (int i = 1; i <= n; i++) {
      int a, b;
      cin >> a >> b;
      doll[i].first = a;
      doll[i].second = b;
    }
    cin >> m;
    for (int i = n + 1; i <= n + m; i++) {
      int a, b;
      cin >> a >> b;
      doll[i].first = a;
      doll[i].second = b;
    }
    sort(doll + 1, doll + 1 + n + m);
    for (int i = 1; i <= n + m; i++) {
      p = lower_bound(dp[0] + 1, dp[0] + n + m + 1, doll[i].first) - dp[0];
      if (dp[1][p] > doll[i].second) {
        dp[0][p] = doll[i].first;
        dp[1][p] = doll[i].second;
      }
    }
    for (int i = 1; i <= n + m; i++) {
      *lower_bound(dp2 + 1, dp2 + n + m + 1, dp[1][i]) = dp[1][i];
    }
    cout << lower_bound(dp2 + 1, dp2 + n + m + 1, INF) - dp2 - 1 << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct Doll {
  int h, r;
  bool operator<(const Doll &a) const { return h < a.h; }
};

int main() {
  int N, M;
  while (cin >> N, N) {
    vector<Doll> d;
    for (int i = 0; i < N; i++) {
      Doll in;
      cin >> in.h >> in.r;
      d.push_back(in);
    }
    cin >> M;
    for (int i = 0; i < M; i++) {
      Doll in;
      cin >> in.h >> in.r;
      d.push_back(in);
    }

    sort(d.begin(), d.end());
    int dp[201][1000] = {}, ans = 0;
    for (int i = 0, cnt = 0; cnt < d.size(); cnt++) {
      if (cnt != 0 && d[cnt - 1].h != d[cnt].h)
        i++;
      for (int j = 0; j < 1000; j++) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        if (j < d[cnt].r)
          ans = max(ans, dp[i + 1][d[cnt].r] =
                             max(dp[i + 1][d[cnt].r], dp[i][j] + 1));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct Doll {
  int h, r;
  bool operator<(const Doll &a) const { return h < a.h; }
};

int main() {
  int N, M;
  while (cin >> N, N) {
    vector<Doll> d;
    for (int i = 0; i < N; i++) {
      Doll in;
      cin >> in.h >> in.r;
      d.push_back(in);
    }
    cin >> M;
    for (int i = 0; i < M; i++) {
      Doll in;
      cin >> in.h >> in.r;
      d.push_back(in);
    }

    sort(d.begin(), d.end());
    int dp[101][1001] = {}, ans = 0;
    for (int i = 0, cnt = 0; cnt < d.size(); cnt++) {
      if (cnt != 0 && d[cnt - 1].h != d[cnt].h)
        i++;
      for (int j = 0; j < d[cnt].r; j++) {
        ans = max(ans,
                  dp[i + 1][d[cnt].r] = max(dp[i + 1][d[cnt].r], dp[i][j] + 1));
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

typedef struct DOLL {
  int h, r;

  bool operator>(const DOLL &d) const { return (h > d.h && r > d.r); }

  bool operator<(const DOLL &d) const { return (h < d.h && r < d.r); }
} D;

typedef struct Node {
  D d;
  int num;
};

const int MAX = 100;

int n, m;
D A[MAX];
D B[MAX];

int AN[MAX];
int BN[MAX];

void solve() {
  queue<Node> q;
  memset(AN, 0, sizeof(int) * n);
  memset(BN, 0, sizeof(int) * m);

  int max = 0;
  Node n1 = {A[0], 1};
  Node n2 = {B[0], 1};
  q.push(n1);
  q.push(n2);
  while (!q.empty()) {
    Node current = q.front();
    q.pop();

    if (max < current.num) {
      max = current.num;
    }

    for (int i = 0; i < n; i++) {
      if (current.d < A[i]) {
        if (AN[i] >= current.num + 1)
          continue;

        Node new_node = {A[i], current.num + 1};
        q.push(new_node);
        AN[i] = current.num + 1;
        break;
      }
    }

    for (int i = 0; i < m; i++) {
      if (current.d < B[i]) {
        if (BN[i] >= current.num + 1)
          continue;

        Node new_node = {B[i], current.num + 1};
        q.push(new_node);
        BN[i] = current.num + 1;
        break;
      }
    }
  }

  cout << max << endl;
}

int main(int argc, char **argv) {
  int h, r;

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> A[i].h >> A[i].r;
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> B[i].h >> B[i].r;
    }

    solve();
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

typedef struct DOLL {
  int h, r;

  bool operator>(const DOLL &d) const { return (h > d.h && r > d.r); }

  bool operator<(const DOLL &d) const { return (h < d.h && r < d.r); }
} D;

typedef struct Node {
  D d;
  int num;
};

const int MAX = 100;

int n, m;
D A[MAX];
D B[MAX];

void solve() {
  queue<Node> q;
  int max = 0;
  Node n1 = {A[0], 1};
  Node n2 = {B[0], 1};
  q.push(n1);
  q.push(n2);
  while (!q.empty()) {
    Node current = q.front();
    q.pop();

    if (max < current.num) {
      max = current.num;
    }

    for (int i = 0; i < n; i++) {
      if (current.d < A[i]) {
        Node new_node = {A[i], current.num + 1};
        q.push(new_node);
        break;
      }
    }

    for (int i = 0; i < m; i++) {
      if (current.d < B[i]) {
        Node new_node = {B[i], current.num + 1};
        q.push(new_node);
        break;
      }
    }
  }

  cout << max << endl;
}

int main(int argc, char **argv) {
  int h, r;

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> A[i].h >> A[i].r;
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> B[i].h >> B[i].r;
    }

    solve();
  }
}
Problem p00157 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

vector<pair<int, int>> v;
int dp[202];
int dfs(int i) {
  if (dp[i] != -1)
    return dp[i];
  int ret = 0;
  for (int j = 0; j < v.size(); j++) {
    if (v[j].first > v[i].first && v[j].second > v[i].second) {
      ret = max(ret, dfs(j) + 1);
    }
  }
  return dp[i] = ret;
}
int main() {
  int n;
  while (cin >> n, n) {
    v.clear();
    int m, x, y, x1, y1, x2, y2, a = 1, c = 1;
    for (int i = 0; i < 202; i++)
      dp[i] = -1;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      v.push_back(make_pair(x, y));
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      v.push_back(make_pair(x, y));
    }
    int ans = 0;
    for (int i = 0; i < m + n; i++) {
      ans = max(ans, dfs(i) + 1);
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define f first
#define s second
#define p(a, b) push_back(make_pair(a, b))
#define g(a, b) for (int i = a; i < b; i++)

int m, n, x, y;
vector<pair<int, int>> v;
int e[101][1001][1001];
int dfs(int i, int x, int y) {
  if (i == v.size())
    return 0;
  if (e[i][x][y] != -1) {
    return e[i][x][y];
  }
  e[i][x][y] =
      max(dfs(i + 1, x, y),
          (v[i].f > x && v[i].s > y) ? dfs(i + 1, v[i].f, v[i].s) + 1 : 0);
  return e[i][x][y];
}
int main() {
  while (cin >> n, n) {
    g(0, 101) for (int j = 0; j < 1001; j++) for (int k = 0; k < 1001; k++)
        e[i][j][k] = -1;
    v.clear();
    g(0, n) {
      cin >> x >> y;
      v.p(x, y);
    }
    cin >> m;
    g(0, m) {
      cin >> x >> y;
      v.p(x, y);
    }
    sort(v.begin(), v.end());
    cout << dfs(0, 0, 0) << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 1000
int x[MAX_N], y[MAX_N], X[MAX_N][MAX_N], dist[MAX_N];
int A, B, N;
int main() {
  while (true) {
    cin >> A;
    if (A == 0) {
      break;
    }
    for (int i = 0; i < A; i++) {
      cin >> x[i] >> y[i];
    }
    cin >> B;
    for (int i = A; i < A + B; i++) {
      cin >> x[i] >> y[i];
    }
    N = A + B;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (x[i] < x[j] && y[i] < y[j]) {
          X[i][j] = 1;
        } else {
          X[i][j] = 0;
        }
      }
    }
    queue<int> Q;
    int res = 0;
    for (int i = 0; i < N; i++) {
      dist[i] = 1;
      Q.push(i);
    }
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      for (int i = 0; i < N; i++) {
        if (X[a1][i] == 1 && dist[a1] + 1 > dist[i]) {
          dist[i] = dist[a1] + 1;
          Q.push(i);
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (res < dist[i]) {
        res = dist[i];
      }
    }
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
#define MAX_N 1000
int x[MAX_N], y[MAX_N], X[MAX_N][MAX_N], dist[MAX_N];
int A, B, N;
int main() {
  while (true) {
    cin >> A;
    if (A == 0) {
      break;
    }
    for (int i = 0; i < A; i++) {
      cin >> x[i] >> y[i];
    }
    cin >> B;
    for (int i = A; i < A + B; i++) {
      cin >> x[i] >> y[i];
    }
    N = A + B;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (x[i] < x[j] && y[i] < y[j]) {
          X[i][j] = 1;
        }
      }
    }
    queue<int> Q;
    int res = 0;
    for (int i = 0; i < N; i++) {
      dist[i] = 0;
      Q.push(i);
    }
    while (!Q.empty()) {
      int a1 = Q.front();
      Q.pop();
      for (int i = 0; i < N; i++) {
        if (X[a1][i] == 1 && dist[a1] + 1 > dist[i]) {
          dist[i] = dist[a1] + 1;
          Q.push(i);
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (res < dist[i]) {
        res = dist[i];
      }
    }
    cout << res << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, m, x, y;

  while (cin >> n && n) {
    vector<pair<int, int>> vec;
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      vec.push_back(make_pair(x, y));
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      vec.push_back(make_pair(x, y));
    }

    int N = n + m;

    vector<int> dp(N, 0);

    sort(vec.begin(), vec.end());

    int ans = 0;

    for (int i = 0; i < N; i++) {
      for (int j = 0; j < i; j++) {
        if (vec[j].first < vec[i].first && vec[j].second < vec[i].second) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
      ans = max(dp[i], ans);
    }

    cout << ans + 1 << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define pb push_back

struct MTR {
  int h, r;
};

int G[1000][1000];

MTR mtr[200];

int dfs(int s, int k, int N) {
  int ret = 0;

  bool last = true;
  for (int i = 0; i < N; i++) {
    if (G[s][i]) {
      last = false;
      ret = max(ret, dfs(i, k + 1, N));
    }
  }
  if (last)
    return k;

  return ret;
}

int main() {

  int n, m;
  while (cin >> n && n) {
    for (int i = 0; i < n; i++) {
      cin >> mtr[i].h >> mtr[i].r;
    }

    cin >> m;
    for (int i = n; i < n + m; i++) {
      cin >> mtr[i].h >> mtr[i].r;
    }

    fill(G[0], G[1000], 0);

    int N = n + m;
    for (int i = 0; i < N - 1; i++) {
      for (int j = 0; j < N; j++) {
        if (mtr[i].h < mtr[j].h && mtr[i].r < mtr[j].r) {
          G[i][j] = 1;
        }
        if (mtr[j].h < mtr[i].h && mtr[j].r < mtr[i].r) {
          G[j][i] = 1;
        }
      }
    }

    int ans = 0;

    for (int i = 0; i < N; i++) {
      ans = max(ans, dfs(i, 1, N));
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX = 1001;
typedef pair<int, int> P;

int main() {
  int n, m, h, r;

  while (cin >> n, n) {
    vector<P> v;
    int dp[MAX] = {0};

    for (int i = 0; i < n; i++) {
      cin >> h >> r;
      v.push_back(P(h, r));
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> h >> r;
      v.push_back(P(h, r));
    }

    sort(v.begin(), v.end());

    int res = 0;
    for (int i = 0; i < n + m; i++) {
      dp[i] = 1;
      for (int j = 0; j < i; j++) {
        if (v[j].first < v[i].first && v[j].second < v[i].second) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
        res = max(res, dp[i]);
      }
    }
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

const int MAX = 101;
typedef pair<int, int> P;

int main() {
  int n, m, h, r;

  while (cin >> n, n) {
    vector<P> v;
    int dp[MAX] = {0};

    for (int i = 0; i < n; i++) {
      cin >> h >> r;
      v.push_back(P(h, r));
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> h >> r;
      v.push_back(P(h, r));
    }

    sort(v.begin(), v.end());

    int res = 0;
    for (int i = 0; i < n + m; i++) {
      dp[i] = 1;
      for (int j = 0; j < i; j++) {
        if (v[j].first < v[i].first && v[j].second < v[i].second) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
        res = max(res, dp[i]);
      }
    }
    cout << res << endl;
  }

  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct doll {
  int r;
  int h;
};
vector<doll> dolls;
int hen[1010][1010];
int dollnum;
int dp[210];

int solve(int n) {
  if (dp[n] != -1) {
    return dp[n];
  }
  int ans = 0;
  int temp = 0;
  for (int i = 0; i < dollnum; i++) {
    if (hen[n][i] == 1) {
      temp = solve(i) + 1;
      ans = max(temp, ans);
    }
  }
  dp[n] = ans;
  return ans;
}
int main() {
  int n, m;
  while (cin >> n) {
    memset(dp, -1, sizeof(dp));
    dolls.clear();
    memset(hen, 0, sizeof(hen));
    if (n == 0)
      break;
    doll temp;
    temp.h = 0;
    temp.r = 0;
    dolls.push_back(temp);
    for (int i = 0; i < n; i++) {
      cin >> temp.h >> temp.r;
      dolls.push_back(temp);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> temp.h >> temp.r;
      dolls.push_back(temp);
    }
    dollnum = dolls.size();
    for (int i = 0; i < dollnum - 1; i++) {
      for (int j = 0; j < dollnum; j++) {
        if (dolls[i].h < dolls[j].h && dolls[i].r < dolls[j].r) {
          hen[i][j] = 1;
        }
        if (dolls[i].h > dolls[j].h && dolls[i].r > dolls[j].r) {
          hen[j][i] = 1;
        }
      }
    }
    cout << solve(0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct doll {
  int r;
  int h;
};
vector<doll> dolls;
int hen[1010][1010];
int dollnum;
int solve(int n) {
  int ans = 0;
  int temp = 0;
  for (int i = 0; i < dollnum; i++) {
    if (hen[n][i] == 1) {
      temp = solve(i) + 1;
      ans = max(temp, ans);
    }
  }

  return ans;
}
int main() {
  int n, m;
  while (cin >> n) {
    dolls.clear();
    memset(hen, 0, sizeof(hen));
    if (n == 0)
      break;
    doll temp;
    temp.h = 0;
    temp.r = 0;
    dolls.push_back(temp);
    for (int i = 0; i < n; i++) {
      cin >> temp.h >> temp.r;
      dolls.push_back(temp);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> temp.h >> temp.r;
      dolls.push_back(temp);
    }
    dollnum = dolls.size();
    for (int i = 0; i < dollnum - 1; i++) {
      for (int j = 0; j < dollnum; j++) {
        if (dolls[i].h < dolls[j].h && dolls[i].r < dolls[j].r) {
          hen[i][j] = 1;
        }
        if (dolls[i].h > dolls[j].h && dolls[i].r > dolls[j].r) {
          hen[j][i] = 1;
        }
      }
    }
    cout << solve(0) << endl;
  }

  return 0;
}