Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		hr := make([]entry, 0)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		sort.Sort(ByFirst(hr))
		fmt.Println(LIS(hr))
	}
}

type entry struct {
	first, second int
}

type ByFirst []entry

func(f ByFirst) Len() int {
	return len(f)
}

func (f ByFirst) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

func (f ByFirst) Less(i, j int) bool {
	return f[i].first < f[j].first
}

func LIS(hr []entry) int {
	n := len(hr)
	lis := make([]int, n)
	lis[n-1] = 1
	for i := n-2; i > -1; i-- {
		m := 0
		for j := i+1; j < n; j++ {
			if hr[i].first < hr[j].first && hr[i].second < hr[j].second && lis[j] > m {
				m = lis[j]
			}
		}
		lis[i] = m+1
	}
	return lis[0]
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
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;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
const int MAX = 100000;
int dp[200];

int dfs(int index, vector<pair<int, int> > &mato){
	if(dp[index]) return dp[index];
	if(!index) return 1;
	int maxc = 0;
	for(int i = index-1; i >= 0; i--){
		if((mato[index].first<=mato[i].first)||(mato[index].second<=mato[i].second)) continue;
		maxc = max(maxc, dfs(i, mato));
	}
	dp[index] = maxc+1;
	return dp[index];
}

void substitute(int time, vector<pair<int, int> > &mato){
	for(int i = 0; i < time; i++){
		pair<int, int> tmp;
		cin >> tmp.first >> tmp.second;
		mato.push_back(tmp);
	}
}

int main(){
	int n, m;
	while(cin >> n && n != 0){
		fill(dp, dp+200, 0);
		vector<pair<int, int> > mato;
		substitute(n, mato);
		cin >> m;
		substitute(m, mato);
		sort(mato.begin(), mato.end());
		int maxc = 0;
		for(int i = n+m-1; i >= 0; i--) maxc = max(maxc, dfs(i, mato));
		cout << maxc << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define H first
#define R second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 201;

int dp[N];
bool ok[N][N];

int main(void){
    for(int n; cin >> n, n;){
        vector<pii> in(N);
        rep(i, n) cin >> in[i].H >> in[i].R;

        int m; cin >> m;
        rep(i, m) cin >> in[n + i].H >> in[n + i].R;

        rep(i, n + m)  dp[i] = 0;
        rep(i, n + m) rep(j, n + m) ok[i][j] = false;

        sort(in.begin(), in.begin() + n + m);

        rep(i, n + m) for(int j = i; j < n + m; j++)
            if(in[i].R < in[j].R) ok[i][j] = true;

        rep(i, n + m) rep(j, i) if(ok[j][i]) dp[i] = max(dp[i], dp[j] + 1);

        cout << *max_element(dp, dp + m + n) + 1 << endl;
    }

    return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
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;
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<cstdlib>
using namespace std;

struct ppp {
  int h;
  int r;
};

int tansaku(vector<ppp> c, vector<ppp> d, int cN, int dN, int ans, int cy, int dy) {
  if(dN >= d.size()) {
    for(int i = cN; i < c.size(); i++) {
      if(d[dy].r < c[i].r && d[dy].h < c[i].h) {
        ans++;
        d[dy] = c[i];
      }
    }
    return ans;
  }
  if(cN >= c.size()) {
    for(int i = dN; i < d.size(); i++) {
      if(d[i].r > c[cy].r && d[i].h > c[cy].h) {
        ans++;
        c[cy] = d[i];
      }
    }
    return ans;
  }
  if(c[cN].h == d[dN].h) {
    return c[cN].r > d[dN].r ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].r == d[dN].r) {
    return c[cN].h > d[dN].h ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].h < d[dN].h && c[cN].r < d[dN].r) {
    return tansaku(c, d, cN + 1, dN, ans + 1, cN, dy);
  }
  if(c[cN].h > d[dN].h && c[cN].r > d[dN].r) {
    return tansaku(c, d, cN, dN + 1, ans + 1, cy, dN);
  }
  if((c[cN].h < d[dN].h && c[cN].r > d[dN].r) || (c[cN].h > d[dN].h && c[cN].r < d[dN].r)) {
    return max(tansaku(c, d, cN + 1, dN, ans + 1, cN, dy), tansaku(c, d, cN, dN + 1, ans + 1, cy, dN));
  }
  return ans;
}

int main() {
  int a, b;
  int j = 0;
  while(cin >> a) {
    j++;
    vector<ppp> c(a);
    for(int i = 0; i < a; i++) {
      cin >> c[i].h >> c[i].r;
    }
    cin >> b;
    vector<ppp> d(b);
    for(int i = 0; i < b; i++) {
      cin >> d[i].h >> d[i].r;
    }
    cout << tansaku(c, d, 0, 0, 0, 0, 0) << endl;
    if(j == 3) break;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int dp[200] = {0};
int h[200] , r[200];
int n,m;

int main(){
	while(cin >> n , n){
		rep(i,n)cin >> h[i] >> r[i];
		cin >> m;
		rep(i,m)cin >> h[n+i] >> r[n+i];
		int ans = 0;
		rep(i,200)dp[i] = 1;
		rep(x,n+m){
			rep(i,n+m)rep(j,n+m){
				if(h[j] < h[i] && r[j] < r[i]){
					dp[i] = max(dp[j]+1,dp[i]);
				}
			}
		}
		cout << *max_element(dp,dp+n+m) << endl;
	}
}