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 <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
#include<iostream>
#include<cmath>

struct Doll {
	int height, radius;
	bool can_wrap(const Doll that) const {
		return height > that.height && radius > that.radius;
	}
};
int max_size(int current, const std::vector<std::vector<int>>& prev, std::vector<int>& memo) {
	if (memo[current] >= 0) return memo[current];
	if (prev[current].empty()) return 1;
	int max = 0;
	for (const auto p : prev[current]) {
		max = std::max(max, max_size(p, prev, memo) + 1);
	}
	return memo[current] = max;
}
int main() {
	while (true) {
		int n; std::cin >> n; if (n == 0) break;
		std::vector<Doll> dolls;
		for (auto i = 0; i < n; ++i) {
			int height, radius; std::cin >> height >> radius;
			dolls.push_back(Doll{ height, radius });
		}
		int m; std::cin >> m;
		for (auto i = 0; i < m; ++i) {
			int height, radius; std::cin >> height >> radius;
			dolls.push_back(Doll{ height, radius });
		}
		std::vector<std::vector<int>> prev(n + m);
		for (auto i = 0; i < n; ++i) {
			if (i != n - 1) prev[i + 1].push_back(i);
			for (auto j = n; j < n + m; ++j) if (dolls[j].can_wrap(dolls[i])) {
				prev[j].push_back(i);
			}
		}
		for (auto i = n; i < n + m; ++i) {
			if (i != n + m - 1) prev[i + 1].push_back(i);
			for (auto j = 0; j < n; ++j) if (dolls[j].can_wrap(dolls[i])){
				prev[j].push_back(i);
			}
		}
		std::vector<int> memo(n + m, -1);
		std::cout << std::max(max_size(n - 1, prev, memo), max_size(n + m - 1, prev, memo)) << '\n';
	}
}

C++: #include <iostream>
#include <algorithm>

class Matryoshika{
public:
    int h, r;

    bool operator<(const Matryoshika& m)const{ return h < m.h; }
    bool operator>(const Matryoshika& m)const{ return h > m.h; }
    bool operator<<(const Matryoshika& m)const{ return h < m.h && r < m.r; }
    bool operator>>(const Matryoshika& m)const{ return h > m.h && r > m.r; }
};

const int MAX_HR = 1001, MAX_NM = 201;
int N, M;
Matryoshika datas[MAX_NM];
int dp[MAX_NM];

int main(){
    while(true){
        std::cin >> N;
        if(N == 0) break;
        for(int i = 0; i < N; ++i){
            std::cin >> datas[i].h >> datas[i].r;
            dp[i] = 1;
        }
        std::cin >> M;
        for(int i = N; i < N + M; ++i){
            std::cin >> datas[i].h >> datas[i].r;
            dp[i] = 1;
        }
        std::sort(datas, datas + (N + M));

        int ans = 0;
        bool flag = true;

        for(int i = 0; i < (N + M - 1); ++i){
            for(int j = i + 1; j < (N + M); ++j){
                if(datas[i] << datas[j]){
                    flag = false;
                    dp[j] = std::max(dp[i] + 1, dp[j]);
                    ans = std::max(dp[j], ans);
                }
            }
        }
        std::cout << ans + flag << std::endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	int n,m;
	vector< pair<int,int> > hr;
	vector< pair<int,int> > rh;
	cin >> n;
	if(n == 0) break;
	REP(i,n){
	    int s,t;
	    cin >> s >> t;
	    hr.PB( pair<int,int>(s,t) );
	    rh.PB( pair<int,int>(t,s) );
	}
	cin >> m;
	REP(i,m){
	    int s,t;
	    cin >> s >> t;
	    hr.PB( pair<int,int>(s,t) );
	    rh.PB( pair<int,int>(t,s) );
	}
	sort(ALL(hr));
	sort(ALL(rh));
	int w,h;
	int k = 0,tempk=0;
	w = 0;
	h = 0;
	REP(i,n+m){
	    if(h < hr[i].first && w < hr[i].second){
		h = hr[i].first;
		w = hr[i].second;
		tempk++;
	    }
	}
	k = tempk;
	tempk = 0;
	w = 0;
	h = 0;
	REP(i,n+m){
	    if(w < rh[i].first && h < rh[i].second){
		w = rh[i].first;
		h = rh[i].second;
		tempk++;
	    }
	}
	k = max(k,tempk);
	cout << k << endl;
    }
    return 0;
}

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


int n, m;
pair<int, int> doll[200];
int dp[200] = { 1 };


int solve(){
	for (int i = 0; i < n + m; i++){
		vector<int> mem = { -1 };
		for (int j = 0; j < i; j++){
			if (doll[j].first < doll[i].first && doll[j].second < doll[i].second){
				mem.push_back(dp[j]);
			}
		}
		if (mem.back() != -1)
			dp[i] = *max_element(mem.begin(), mem.end()) + 1;
	}
	return *max_element(dp, dp + n + m);
}


void clear(){
	for (int i = 0; i < 200; i++){
		doll[i] = { 0, 0 };
		dp[i] = 0;
	}
	dp[0] = 1;
}


int main(){
	while (cin >> n, n){
		for (int i = 0; i < n; i++) cin >> doll[i].first >> doll[i].second;
		cin >> m;
		for (int i = n; i < n + m; i++) cin >> doll[i].first >> doll[i].second;

		sort(doll, doll + n + m);

		cout << solve() << endl;

		clear();
	}
	
	return 0;
}

C++: #include <cstring>
#include <iostream>
using namespace std;
int n;
int h[200], r[200];
int memo[200];

int solve(int x) {
	if (memo[x] != -1) return memo[x];
	int res = 0;
	for (int i = 0; i < n; i++) {
		if (h[x] < h[i] && r[x] < r[i]) {
			res = max(res, solve(i));
		}
	}
	return memo[x] = (res + 1);
}

int main () {
	while (cin >> n, n) {
		memset(memo, -1, sizeof(memo));
		for (int i = 0; i < n; i++) {
			cin >> h[i] >> r[i];
		}
		int m;
		cin >> m;
		for (int i = 0; i < m; i++) {
			cin >> h[n + i] >> r[n + i];
		}
		n += m;
		int res = 0;
		for (int i = 0; i < n; i++) {
			res = max(res, solve(i));
		}
		cout << res << endl;
	}
	return 0;
}