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

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

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

const int SIZE = 201;
const int NONE = -1;

int H[SIZE];
int R[SIZE];
int N;
int dp[1002][1002];

int solve(int hh, int rr) {
  int &res = dp[hh][rr];
  if (res != NONE)
    return res;
  int maxd = 0;
  for (int i = 0; i < N; ++i) {
    if (H[i] < hh && R[i] < rr) {
      maxd = max(maxd, 1 + solve(H[i], R[i]));
    }
  }
  return res = maxd;
}

int main() {
  int n, m;
  while (cin >> n && n) {
    for (int i = 0; i < 1002; ++i) {
      for (int j = 0; j < 1002; ++j) {
        dp[i][j] = NONE;
      }
    }
    for (int i = 0; i < n; ++i) {
      cin >> H[i] >> R[i];
    }
    cin >> m;
    for (int i = n; i < n + m; ++i) {
      cin >> H[i] >> R[i];
    }
    N = n + m;
    cout << solve(1001, 1001) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair<INT, INT> II;
typedef vector<II> VII;

template <class T> ostream &operator<<(ostream &os, vector<T> v) {
  for (typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i) {
    os << *it_i << ", ";
  }
  return os;
}

const int SIZE = 101;

int H[SIZE];
int R[SIZE];
int N;

int solve(int hh, int rr) {
  int res = 0;
  for (int i = 0; i < N; ++i) {
    if (H[i] < hh && R[i] < rr)
      res = max(res, 1 + solve(H[i], R[i]));
  }
  return res;
}

int main() {
  int n, m;
  while (cin >> n && n) {
    for (int i = 0; i < n; ++i) {
      cin >> H[i] >> R[i];
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
      cin >> H[n + i] >> R[n + i];
    }
    N = n + m;
    cout << solve(INT_MAX, INT_MAX) << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dp[200];
vector<pair<int, int>> m;

int main() {
  while (true) {
    fill(dp, dp + 200, -1);
    m.clear();

    for (int i = 0; i < 2; i++) {
      int a;
      cin >> a;
      if (!a)
        return 0;
      for (int j = 0; j < a; j++) {
        int x, y;
        cin >> x >> y;
        m.push_back(make_pair(x, y));
      }
    }
    sort(m.begin(), m.end());
    int ans = 0;
    for (int i = 0; i < m.size(); i++) {
      dp[i] = 1;
      for (int j = 0; j < i; j++)
        if (m[j].first < m[i].first && m[j].second < m[i].second)
          dp[i] = max(dp[i], dp[j] + 1);
      ans = max(ans, dp[i]);
    }
    cout << ans << endl;
    ;
  }
}

Error: TLE

Fixed submission:

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

int N;

int m[200][2];
vector<pair<pair<int, int>, int>> m1, m2;
vector<int> pa[200];

int dfs(int x) {
  int r = 0;
  for (auto v : pa[x])
    r = max(r, dfs(v));
  return r + 1;
}

int main() {
  while (true) {
    for (int i = 0; i < 200; i++)
      pa[i].clear(), (m[i][0] = m[i][1] = 0);
    m1.clear();
    m2.clear();

    N = 0;
    for (int i = 0; i < 2; i++) {
      int a;
      cin >> a;
      if (!a)
        return 0;
      for (int j = N; j < a + N; j++) {
        cin >> m[j][0] >> m[j][1];
        m1.push_back(make_pair(make_pair(m[j][0], m[j][1]), j));
        m2.push_back(make_pair(make_pair(m[j][1], m[j][0]), j));
      }
      N += a;
    }
    sort(m1.begin(), m1.end());
    sort(m2.begin(), m2.end());
    for (int i = 0; i < N; i++) {
      bool b1 = false, b2 = false;
      for (int j = i + 1; j < N; j++) {
        if (!b1 && m1[j].first.second > m1[i].first.second)
          pa[m1[i].second].push_back(m1[j].second), b1 = true;
        if (!b2 && m2[j].first.second > m2[i].first.second)
          pa[m2[i].second].push_back(m2[j].second), b1 = true;
      }
    }
    cout << max(dfs(m1[0].second), dfs(m2[0].second)) - 1 << endl;
    ;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m, h, r;
  while (cin >> n, n) {
    vector<pair<int, int>> v;
    for (int i = 0; i < n; ++i) {
      cin >> h >> r;
      v.push_back(make_pair(h, r));
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
      cin >> h >> r;
      v.push_back(make_pair(h, r));
    }
    sort(v.begin(), v.end());
    int mx = 0, dp[200];
    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);
        }
      }
      mx = max(mx, dp[i]);
    }
    cout << mx << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, m, h, r;
  while (cin >> n, n) {
    vector<pair<int, int>> v;
    for (int i = 0; i < n; ++i) {
      cin >> h >> r;
      v.push_back(make_pair(h, r));
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
      cin >> h >> r;
      v.push_back(make_pair(h, r));
    }
    sort(v.begin(), v.end());
    int mx = 0, dp[100];
    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);
        }
      }
      mx = max(mx, dp[i]);
    }
    cout << mx << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
#define pint pair<int, int>
int main() {
  int n, m;
  while (cin >> n, n) {
    pint p[200];
    rep(i, n) { cin >> p[i].first >> p[i].second; }
    cin >> m;
    rep(i, m) { cin >> p[n + i].first >> p[n + i].second; }
    sort(p, p + m + n);
    int dp[200], ret = 0;
    rep(i, 200) dp[i] = 1;
    rep2(i, 1, m + n) {
      rep(j, i) {
        if (p[j].first < p[i].first && p[j].second < p[i].second) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
      ret = max(ret, dp[i]);
    }
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define rep2(x, from, to) for (int x = (from); x < (to); ++(x))
#define rep(x, to) rep2(x, 0, to)
#define pint pair<int, int>
void dp(vector<vector<int>> v, int depth, int s, int &ret) {
  if (v[s].size() == 0) {
    if (ret < depth)
      ret = depth;
    return;
  }
  rep(i, v[s].size()) { dp(v, depth + 1, v[s][i], ret); }
}
int main() {
  int n, m, x, y;
  while (cin >> n, n) {
    vector<pint> a;
    rep(i, n) {
      cin >> x >> y;
      a.push_back(make_pair(x, y));
    }
    cin >> m;
    rep(i, m) {
      cin >> x >> y;
      a.push_back(make_pair(x, y));
    }
    sort(a.begin(), a.end());
    vector<vector<int>> v(n + m);
    rep(i, n + m) {
      rep(j, n + m) {
        if (a[i].first < a[j].first && a[i].second < a[j].second) {
          v[i].push_back(j);
        }
      }
    }
    int ret = 0;
    rep(i, n + m) { dp(v, 1, i, ret); }
    cout << ret << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct DOLL {
  int h;
  int r;
};

int memo[1001][1001];

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r, int i1, int i2) {
  if (memo[r][h] >= 0) {
    return memo[r][h];
  }

  int ans = 0;

  for (int i = i1; i < d1.size(); i++) {
    DOLL &d = d1[i];
    if (d.r > r && d.h > h) {
      int a = f(d1, d2, d.h, d.r, i + 1, i2) + 1;
      ans = max(a, ans);
      break;
    }
  }

  for (int i = i2; i < d2.size(); i++) {
    DOLL &d = d2[i];
    if (d.r > r && d.h > h) {
      int a = f(d1, d2, d.h, d.r, i1, i2 + 1) + 1;
      ans = max(a, ans);
      break;
    }
  }

  memo[r][h] = ans;

  return ans;
}

int main() {

  while (true) {

    for (int i = 0; i < 1001; i++) {
      fill(memo[i], memo[i] + 1001, -1);
    }

    vector<DOLL> d1;
    vector<DOLL> d2;
    int n, m;

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      DOLL d;
      cin >> d.h >> d.r;
      d1.push_back(d);
    }

    cin >> m;

    for (int i = 0; i < m; i++) {
      DOLL d;
      cin >> d.h >> d.r;
      d2.push_back(d);
    }

    int ans = f(d1, d2, 0, 0, 0, 0);
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

struct DOLL {
  int h;
  int r;
};

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r, int i0, int i1) {
  int ans = 0;
  for (int i = i0; i < d1.size(); i++) {
    DOLL &d = d1[i];
    if (d.h > h && d.r > r) {
      int a = f(d1, d2, d.h, d.r, i + 1, i1) + 1;
      ans = max(a, ans);
      break;
    }
  }
  for (int i = i1; i < d2.size(); i++) {
    DOLL &d = d2[i];
    if (d.h > h && d.r > r) {
      int a = f(d1, d2, d.h, d.r, i0, i + 1) + 1;
      ans = max(a, ans);
      break;
    }
  }
  return ans;
}

int main() {

  while (true) {
    vector<DOLL> d1;
    vector<DOLL> d2;
    int n, m;

    cin >> n;
    if (n == 0)
      break;

    for (int i = 0; i < n; i++) {
      DOLL d;
      cin >> d.h >> d.r;
      d1.push_back(d);
    }

    cin >> m;

    for (int i = 0; i < m; i++) {
      DOLL d;
      cin >> d.h >> d.r;
      d2.push_back(d);
    }

    int ans = f(d1, d2, 0, 0, 0, 0);
    cout << ans << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  int n, m;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    int h, r;
    int mato[1001][1001] = {};
    for (int i = 0; i < n; i++) {
      cin >> h >> r;
      mato[h][r] = 1;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> h >> r;
      mato[h][r] = 1;
    }
    int result[1001][1001] = {};
    int ans = 0;
    for (int i = 1; i < 1001; i++) {
      for (int j = 1; j < 1001; j++) {
        result[i][j] = max(result[i - 1][j - 1] + mato[i][j],
                           max(result[i - 1][j], result[i][j - 1]));
        ans = max(ans, result[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

int main() {
  int n, m;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    vector<pair<int, int>> mato(100);
    for (int i = 0; i < n; i++) {
      cin >> mato[i].first >> mato[i].second;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> mato[n + i].first >> mato[n + i].second;
    }
    sort(mato.begin(), mato.begin() + m + n);
    int cnt = 0;
    int maxh = 0, maxr = 0;
    for (int i = 0; i < n + m; i++) {
      // cout << mato[i].first << " " << mato[i].second;
      if (mato[i].first > maxh && mato[i].second > maxr) {
        cnt++;
        maxh = mato[i].first;
        maxr = mato[i].second;
      }
      // cout << " " << cnt << endl;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<int, int> pii;
int dptab[256][1024] = {0};

int main() {
  int n, m;
  while (cin >> n, n) {
    vector<pii> v;
    for (int i = 0; i < n; i++) {
      pii tmp;
      cin >> tmp.first >> tmp.second;
      v.push_back(tmp);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      pii tmp;
      cin >> tmp.first >> tmp.second;
      v.push_back(tmp);
    }
    sort(v.begin(), v.end());

    for (int i = 0; i < 256; i++)
      for (int j = 0; j < 1024; j++)
        dptab[i][j] = 0;

    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < 1024; j++) {
        if (i > 0 && v[i].first == v[i - 1].first) {
          dptab[i + 1][j] = max(
              dptab[i][j], v[i].second < j ? dptab[i - 1][v[i].second] + 1 : 0);
        } else {
          dptab[i + 1][j] =
              max(dptab[i][j], v[i].second < j ? dptab[i][v[i].second] + 1 : 0);
        }
      }
    }
    cout << dptab[n + m][1010] << endl;
  }
}

Error: 0

Fixed submission:

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

typedef pair<int, int> pii;
int dptab[128][1024] = {0};

int main() {
  int n, m;
  while (cin >> n, n) {
    vector<pii> v;
    for (int i = 0; i < n; i++) {
      pii tmp;
      cin >> tmp.first >> tmp.second;
      v.push_back(tmp);
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      pii tmp;
      cin >> tmp.first >> tmp.second;
      v.push_back(tmp);
    }
    sort(v.begin(), v.end());

    for (int i = 0; i < 128; i++)
      for (int j = 0; j < 1024; j++)
        dptab[i][j] = 0;

    for (int i = 0; i < v.size(); i++) {
      for (int j = 0; j < 1024; j++) {
        if (i > 0 && v[i].first == v[i - 1].first) {
          dptab[i + 1][j] = max(
              dptab[i][j], v[i].second < j ? dptab[i - 1][v[i].second] + 1 : 0);
        } else {
          dptab[i + 1][j] =
              max(dptab[i][j], v[i].second < j ? dptab[i][v[i].second] + 1 : 0);
        }
      }
    }
    cout << dptab[n + m][1000] << endl;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

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

class Doll {
public:
  int h;
  int r;
  int have;
  // static bool Less(const Doll& l, const Doll& r){
  // 	return l.h < r.h;
  // }
  bool operator<(const Doll &d) const {
    if (r == d.r)
      return h < d.h;
    else
      return r < d.r;
  }
  Doll(int x, int y, int z) {
    h = x;
    r = y;
    have = z;
  }
};

typedef vector<Doll> Dolls;

int main() {
  int n, m, h, r, result = 0;
  Dolls dolls;
  while (cin >> n, n) {
    for (int i = 0; i < n; ++i) {
      cin >> h >> r;
      Doll temp(h, r, 1);
      dolls.push_back(temp);
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
      cin >> h >> r;
      Doll temp(h, r, 1);
      dolls.push_back(temp);
    }
    //	sort(dolls.begin(), dolls.end(), Doll::Less);
    sort(dolls.begin(), dolls.end());
    for (Dolls::iterator it1 = dolls.begin(); it1 != dolls.end(); ++it1) {
      for (Dolls::iterator it2 = dolls.begin(); it2 != it1; ++it2) {
        if (it2->h < it1->h && it2->r < it1->r && it1->have <= it2->have) {
          it1->have = it2->have + 1;
          //		    result = max(result, it1->have);
        }
      }
    }
    for (Dolls::iterator it = dolls.begin(); it != dolls.end(); ++it)
      result = max(result, it->have);
    cout << result << endl;
    dolls.clear();
    result = 0;
  }
  return 0;
}

Error: 0
9
6
8


Fixed submission:

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

class Doll {
public:
  int h;
  int r;
  int dp;
  static bool Less(const Doll &l, const Doll &r) { return l.h < r.h; }
  Doll(int x, int y, int z) {
    h = x;
    r = y;
    dp = z;
  }
};

typedef vector<Doll> Dolls;

int main() {
  int n, m, h, r, result = 0;
  Dolls dolls;
  while (cin >> n, n) {
    for (int i = 0; i < n; ++i) {
      cin >> h >> r;
      Doll temp(h, r, 1);
      dolls.push_back(temp);
    }
    cin >> m;
    for (int i = 0; i < m; ++i) {
      cin >> h >> r;
      Doll temp(h, r, 1);
      dolls.push_back(temp);
    }
    sort(dolls.begin(), dolls.end(), Doll::Less);

    for (Dolls::iterator it1 = dolls.begin() + 1; it1 != dolls.end(); ++it1) {
      for (Dolls::iterator it2 = dolls.begin(); it2 != it1; ++it2) {
        if (it2->h < it1->h && it2->r < it1->r && it1->dp <= it2->dp) {
          it1->dp = it2->dp + 1;
          result = max(result, it1->dp);
        }
      }
    }
    cout << result << endl;
    cerr << result << endl;
    dolls.clear();
    result = 0;
  }
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main() {
  int n, m;
  int dp[2000];
  P doll[2000];
  while (cin >> n && n) {
    fill(doll, doll + 2000, P(0, 0));
    fill(dp, dp + 2000, INF);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      doll[i] = P(-a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++) {
      int a, b;
      cin >> a >> b;
      doll[i] = P(-a, b);
    }
    sort(doll, doll + n + m, greater<P>());
    for (int i = 0; i < n + m; i++) {
      *lower_bound(dp, dp + 2000, doll[i].S) = doll[i].S;
    }
    int ans;
    for (int i = n + m - 1; i >= 0; --i) {
      if (dp[i] != INF) {
        ans = i + 1;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main() {
  int n, m;
  int dp[201], dp2[201];
  P doll[200];
  while (cin >> n && n) {
    fill(doll, doll + 200, P(0, 0));
    fill(dp, dp + 201, INF);
    fill(dp2, dp + 201, 0);
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++) {
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    dp[0] = doll[0].S;
    for (int i = 1; i < n + m; i++) {
      int *p;
      p = lower_bound(dp, dp + 201, doll[i].S);
      if (doll[i].F != dp2[p - 1 - dp]) {
        *p = doll[i].S;
        dp2[p - dp] = doll[i].F;
      }
    }
    cout << lower_bound(dp, dp + 201, INF) - dp << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n[2], c[2];
  while (cin >> n[0], n[0]) {
    int h[2][110], r[2][110], mx[2][110];
    rep(q, 2) {
      rep(i, n[q]) mx[q][i] = 0, cin >> h[q][i] >> r[q][i];
      if (!q)
        cin >> n[1];
    }
    mx[0][0] = (h[0][0] > h[1][0] && r[0][0] > r[1][0]) + 1;
    mx[1][0] = (h[0][0] < h[1][0] && r[0][0] < r[1][0]) + 1;
    int p;
    c[0] = c[1] = p = 0;
    while (c[0] < n[0] || c[1] < n[1]) {
      int tmax = 0, f;
      if (c[p] == n[p]) {
        p ^= 1;
        goto NEXT;
      }
      rep(k, 2) {
        rep(i, n[k]) {
          f = h[k][i] < h[p][c[p]] && r[k][i] < r[p][c[p]];
          if (f && tmax < mx[k][i])
            tmax = mx[k][i];
          if (f && mx[k][i] == 0) {
            p ^= 1;
            goto NEXT;
          }
        }
      }
      mx[p][c[p]++] = tmax + 1;
    NEXT:;
    }
    cout << max(mx[0][n[0] - 1], mx[1][n[1] - 1]) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n[2], c[2];
  while (cin >> n[0], n[0]) {
    int h[2][110], r[2][110], mx[2][110];
    rep(q, 2) {
      rep(i, n[q]) mx[q][i] = 0, cin >> h[q][i] >> r[q][i];
      if (!q)
        cin >> n[1];
    }
    mx[0][0] = (h[0][0] > h[1][0] && r[0][0] > r[1][0]) + 1;
    mx[1][0] = (h[0][0] < h[1][0] && r[0][0] < r[1][0]) + 1;
    int p;
    c[0] = c[1] = p = 0;
    while (c[0] < n[0] || c[1] < n[1]) {
      int tmax = 0, f;
      rep(k, 2) {
        rep(i, n[k]) {
          f = h[k][i] < h[p][c[p]] && r[k][i] < r[p][c[p]];
          if (f && tmax < mx[k][i])
            tmax = mx[k][i];
          if (f && mx[k][i] == 0) {
            p ^= 1;
            goto NEXT;
          }
        }
      }
      mx[p][c[p]++] = tmax + 1;
    NEXT:;
    }
    cout << max(mx[0][n[0] - 1], mx[1][n[1] - 1]) << endl;
  }
  return 0;
}
Problem p00157 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#define F first
#define S second
using namespace std;

int n, m, i;
pair<int, int> p[222];
int memo[222];

int solve(int k) {

  if (memo[k])
    return memo[k];

  int ret = 1;
  for (int i = 0; i < n + m; i++) {
    if (p[k].F > p[i].F && p[k].S > p[i].S)
      ret = max(ret, solve(i) + 1);
  }

  return memo[k] = ret;
}

int main() {

  while (cin >> n, n) {

    for (i = 0; i < n; i++)
      cin >> p[i].F >> p[i].S;
    cin >> m;
    for (; i < m + n; i++)
      cin >> p[i].F >> p[i].S;

    memset(memo, 0, sizeof(memo));

    int ans = 0;
    for (int j = 0; j < n + m; j++)
      ans = max(solve(j), ans);

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#define F first
#define S second
using namespace std;

int n, m, i;
pair<int, int> p[222];

int solve(int k, int h, int r) {

  int ret = 0;
  for (int i = k; i < n + m; i++) {
    if (h < p[i].F && r < p[i].S)
      ret = max(ret, solve(i + 1, p[i].F, p[i].S) + 1);
  }

  return ret;
}

int main() {

  while (cin >> n) {

    for (i = 0; i < n; i++)
      cin >> p[i].F >> p[i].S;
    cin >> m;
    for (; i < m + n; i++)
      cin >> p[i].F >> p[i].S;

    sort(p, p + n + m);

    cout << solve(0, 0, 0) << endl;
  }
}