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 <bits/stdc++.h>
using namespace std;

struct doll{
    int h,r;
};

bool operator<(const doll& d1,const doll& d2){
    if(d1.h == d2.h){
        return d1.r < d2.r;
    }
    return d1.h < d2.h;
}

int main() {

    while(1){

        int n;

        cin >> n;

        if(n==0) break;

        int h,r;

        vector<doll> v;
        doll tmp;

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

        int m;

        cin >> m;

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

        /*for(int i=0;i<n+m;i++){
            cout << v[i].h << " " << v[i].r << endl;
        }*/

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

        int table[201][201];

        for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                table[i][j] = 0;
            }
        }

        /*for(int i=0;i<n+m;i++){
            table[i][i] = 1;
        }*/

        for(int i=0;i<n+m;i++){
            for(int j=i;j<n+m;j++){
                if( (v[i].h<v[j].h && v[i].r < v[j].r) || (v[i].h > v[j].h && v[i].r > v[j].r) ){
                    table[i][j] = 1;
                    table[j][i] = 1;
                }
            }
        }

        /*for(int i=0;i<n+m;i++){
            for(int j=0;j<n+m;j++){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/

        int dp[201][201] = {0};

        for(int i=0;i<=n+m;i++){
            dp[i][0] = 0;
            dp[0][i] = 1;
        }

        for(int i=1;i<=n+m;i++){
            for(int j=1;j<=n+m;j++){
                if(table[i-1][j-1]==1){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+1);
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
            }

        }

        /*for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/

        int ans = 0;

        for(int i=0;i<=n+m;i++){
            //cout << dp[i] << endl;
            ans = max(dp[i][n+m],ans);
        }

        cout << ans << endl;


    }

    return 0;
}

C++: #include <iostream>
#include <map>
#include <vector>

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		map<int, int> data;
		for(int i = 0; i < n; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		vector<P> doll;
		map<int, int>::reverse_iterator rit = data.rbegin();
		while(rit != data.rend())
		{
			doll.push_back(P(rit->first, rit->second));
			++rit;
		}
		int max_doll_count = 0;
		for(int i = 0; i < doll.size(); ++i)
		{
			int doll_count = 1;
			P current = doll[i];
			for(int j = i + 1; j < doll.size(); ++j)
			{
				if(current.first > doll[j].first && current.second > doll[j].second)
				{
					++doll_count;
					current = doll[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}
		
		vector<P> doll2;
		map<int, int>::reverse_iterator rit2 = data.rbegin();
		while(rit2 != data.rend())
		{
			doll2.push_back(P(rit2->second, rit2->first));
			++rit2;
		}
		for(int i = 0; i < doll2.size(); ++i)
		{
			int doll_count = 1;
			P current = doll[i];
			for(int j = i + 1; j < doll2.size(); ++j)
			{
				if(current.first > doll2[j].first && current.second > doll2[j].second)
				{
					++doll_count;
					current = doll2[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}
		cout << max_doll_count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) for (int i=0; i<(int)(n); ++i)
#define FOREQ(i,k,n) for (int i=(k); i<=(int)(n); ++i)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FIND(m,w) ((m).find(w)!=(m).end())
map<PII,int> memo;
int dfs(int h,int r,int n,vector<PII>& ich,vector<PII>& ji){
  if(memo.find(PII(h,r))!=memo.end()){
    int m = memo[PII(h,r)];
    if(m>=n) return m;
  }
  memo[PII(h,r)] = n;
  int ret=n;
  REP(i,ich.size()){
    if(ich[i].first<h&&ich[i].second<r){
      ret = max(ret,dfs(ich[i].first,ich[i].second,n+1,ich,ji));
      break;
    }
  }
  REP(i,ji.size()){
    if(ji[i].first<h&&ji[i].second<r){
      ret = max(ret,dfs(ji[i].first,ji[i].second,n+1,ich,ji));
      break;
    }
  }
  return ret;
}
int main(void){
  int n,m;
  while(cin>>n,n){
    memo.clear();
    vector<PII> ich;
    vector<PII> ji;
    REP(i,n){
      int h,r;
      cin>>h>>r;
      ich.push_back(PII(h,r));
    } 
    cin>>m;
    REP(i,m){
      int h,r;
      cin>>h>>r;
      ji.push_back(PII(h,r));
    }
    reverse(ich.begin(),ich.end());
    reverse(ji.begin(),ji.end());
    cout<<dfs(INF,INF,0,ich,ji)<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }

void input_dolls(vector<pair<int, int>> &dolls, int n) {
	for(int i = 0; i < n; ++i) {
		int h, r;
		cin >> h >> r;
		dolls.emplace_back(h, r);
	}
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int n; cin >> n && n;) {
		vector<pair<int, int>> dolls;
		input_dolls(dolls, n);

		int m;
		cin >> m;
		input_dolls(dolls, m);

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

		int prev_h = -1;
		vector<int> dp(1000, 0);
		vector<int> tmp(dp);

		for(const auto &doll : dolls) {
			const int h = doll.first;
			const int r = doll.second;

			if(prev_h < h) {
				tmp = dp;
				prev_h = h;
			}

			const int num = *max_element(tmp.begin(), tmp.begin() + r);
			chmax(dp[r], num + 1);
		}

		cout << *max_element(dp.begin(), dp.end()) << endl;
	}

	return EXIT_SUCCESS;
}