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


class doll;


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


class doll{
public:
	int h;
	int r;
	doll(int h, int r) : h(h), r(r){}
	int operator[](char member){
		if (member == 'h') return h;
		else if(member == 'r') return r;
		else return -1;
	}
};


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


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


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

		sort(d.begin(), d.end(), [](doll& d1, doll& d2){
			return (d1.h < d2.h);
		});

		int ans = solve('r');

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

		sort(d.begin(), d.end(), [](doll& d1, doll& d2){
			return (d1.r < d2.r);
		});

		ans = max(ans, solve('h'));

		cout << ans << endl;

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

	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

int n, m;
int h1[100], h2[100], r1[100], r2[100];
int next1[100], next2[100];

int calc(int idx, int d)
{
	static int dp[2][100];
	
	if(idx == 0 && d == 0)
		for(int i = 0; i < 2; ++i)
			for(int j = 0; j < max(n, m); ++j)
				dp[i][j] = -1;	
	
	int ret = 0;
	
	if((idx < 0) || (d == 0 && idx >= n) || (d == 1 && idx >= m))
		return 0;
		
	if(dp[d][idx] != -1)
		return dp[d][idx];
	
	ret = calc(idx + 1, d);
	if(d == 0)
		ret = max(ret, calc(next1[idx], 1));
	else
		ret = max(ret, calc(next2[idx], 0));
	
	return dp[d][idx] = ret + 1;
}

int main()
{
	while(true) {
	
		int idx;
		
		scanf("%d", &n);
		if(n == 0)
			break;
		for(int i = 0; i < n; ++i)
			scanf("%d%d", &h1[i], &r1[i]), next1[i] = -1;
		scanf("%d", &m);
		if(m == 0)
			break;
		for(int i = 0; i < m; ++i)
			scanf("%d%d", &h2[i], &r2[i]), next2[i] = -1;
		
		idx = 0;
		for(int i = 0; i < n; ++i) {
			
			while(idx < m) {
				if(h1[i] < h2[idx] && r1[i] < r2[idx])
					break;
				++idx;
			}
			if(idx >= m)
				break;
			next1[i] = idx;
		}
		
		idx = 0;
		for(int i = 0; i < m; ++i) {
			
			while(idx < n) {
				if(h2[i] < h1[idx] && r2[i] < r1[idx])
					break;
				++idx;
			}
			if(idx >= n)
				break;
			next2[i] = idx;
		}
		
		int ans = max(calc(0, 0), calc(0, 1));
		
		printf("%d\n", ans);
	}
	
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<pair<int, int> > doll(n);
        for(int i=0; i<n; ++i)
            cin >> doll[i].first >> doll[i].second;

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

        sort(doll.begin(), doll.end());
        vector<int> num(n+m, 1);
        for(int i=0; i<n+m; ++i){
            for(int j=0; j<i; ++j){
                if(doll[j].first < doll[i].first && doll[j].second < doll[i].second)
                    num[i] = max(num[i], num[j] + 1);
            }
        }
        cout << *max_element(num.begin(), num.end()) << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define f first
#define s second
#define pb(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)
int main(){
    int n;
    while(cin>>n,n){
        vector<pair<int,int> >v;
        int m,x,y,a=1,c=1,b=201;
        g(0,n){
            cin>>x>>y;
            v.pb(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.pb(x, y);
        }
        sort(v.begin(),v.end());
        x=v[0].f,y=v[0].s;
        g(1,m+n){
            if(v[i].s>y&&v[i].f>x){
                a++;
                x=v[i].f,y=v[i].s;
            }
            else
                b=min(b,i);
        }
        if(b<201){
            x=v[b].f,y=v[b].s;
            g(b+1,m+n){
                if(v[i].s>y&&v[i].f>x){
                    c++;
                    x=v[i].f,y=v[i].s;
                }
            }
        }
        cout<<max(a,b+c)<<endl;
    }
}

C++: #include <iostream>

using namespace std;

int n, m;
int d[200][2], dp[200];

int main()
{
    for(;;){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        int a = d[j][0];
                        int b = d[j][1];
                        d[j][0] = d[j - 1][0];
                        d[j][1] = d[j - 1][1];
                        d[j - 1][0] = a;
                        d[j - 1][1] = b;
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        int a = d[j][0];
                        int b = d[j][1];
                        d[j][0] = d[j - 1][0];
                        d[j][1] = d[j - 1][1];
                        d[j - 1][0] = a;
                        d[j - 1][1] = b;
                    }
                }
            }
            int max = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]&&d[j][1] < d[i][1]){
                        a = dp[j];
                    }
                }
                dp[i] += a;
                if(max <= dp[i]){
                    max = dp[i];
                }
            }
            cout << max << endl;
        }else{
            break;
        }
    }
}