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] = 1;
            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 <algorithm>
#include <vector>
#include <utility>
using namespace std;


int n, m;
vector<pair<int, int>> d;
vector<int> dp = { 0 };


int solve(){
	dp[0] = d[0].second;
	for (int i = 1; i < d.size(); i++){
		if (dp.back() < d[i].second){
			dp.push_back(d[i].second);
		}
		else{
			*lower_bound(dp.begin(), dp.end(), d[i].second) = d[i].second;
		}
	}
	return dp.size();
}


template<typename T>
void clear(vector<T>& v){
	vector<T> emp;
	swap(v, emp);
}


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

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

		int ans = solve();

		cout << ans << endl;

		clear(d);
		clear(dp);
		dp.push_back(0);
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

typedef pair<int, int> Doll;
vector<Doll> Dolls;
int dp[201];

void debug(){
  for(int i = 0; i < Dolls.size(); i++){
    cout << "(" << Dolls[i].first << ", " << Dolls[i].second << ")" << endl;
  }
}

bool comp(const Doll& d1, const Doll& d2){
  return ((d1.first <= d2.first) && (d1.second <= d2.second));
}

int main(void){
  int n, m;
  while(cin >> n, n){
    Dolls.clear();
    Dolls.push_back(Doll(0, 0));
    for(int i = 0; i < n; i++){
      int h, r;
      cin >> h >> r;
      Dolls.push_back(Doll(h, r));
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      int h, r;
      cin >> h >> r;
      Dolls.push_back(Doll(h, r));
    }
    sort(Dolls.begin(), Dolls.end(), comp);
    for(int i = 1; i < Dolls.size(); i++){
      int max_k = 0;
      for(int j = 0; j < i; j++){
        if(Dolls[j].first < Dolls[i].first && Dolls[j].second < Dolls[i].second && dp[max_k] < dp[j]){
          max_k = j;
        }
      }
      dp[i] = dp[max_k] + 1;
    }
    cout << dp[Dolls.size()-1] + 1 << endl;
  }
  return 0;
}

C++: #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();
	}
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>

using namespace std;

bool exist[2000][2000];
int max_doll[2000][2000];

int main(){
	
	int n, m;
	
	while(true){
		
		cin >> n;
		
		if(n == 0){
			break;
		}
		
		for(int i = 0; i <= 1000; i++){
			for(int j = 0; j <= 1000; j++){
				exist[i][j] = false;
				max_doll[i][j] = 0;
			}
		}
		
		int h, r;
		
		for(int i = 0; i < n; i++){
			cin >> h >> r;
			exist[h][r] = true;
		}
		
		cin >> m;
		
		for(int i = 0; i < m; i++){
			cin >> h >> r;
			exist[h][r] = true;
		}
		
		for(int i = 1; i <= 1000; i++){
			for(int j = 1; j <= 1000; j++){
				if(exist[i][j]){
					max_doll[i][j] = max(max(max_doll[i - 1][j], max_doll[i][j - 1]), max_doll[i - 1][j - 1] + 1);
				}else{
					max_doll[i][j] = max(max_doll[i - 1][j], max_doll[i][j - 1]);
				}
			}
		}
		
		cout << max_doll[1000][1000] << endl;
		
	}
	return 0;
}