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;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> Pi;

int N, M;

int main() {
	while (scanf("%d", &N), N) {
		vector<P> state;
		int dp[212];
		int h, r;
		rep(i, N) {
			scanf("%d %d", &h, &r);
			state.push_back(P(h, r));
		}
		scanf("%d", &M);
		rep(i, M) {
			scanf("%d %d", &h, &r);
			state.push_back(P(h, r));
		}
		sort(state.begin(), state.end());
		int res = 0;
		rep(i, state.size()) {
			dp[i] = 1;
			rep(j, i) if(state[j].first < state[i].first && state[j].second < state[i].second) {
				dp[i] = max(dp[i], dp[j] + 1);
			}
		}
		rep(i, state.size()) {
			res = max(res, dp[i]);
		}
		printf("%d\n", res);
	}
	return 0;
}

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

int n,m;

typedef pair<int,int> P;

int calc(P d[1000],int c)
{
	int res = 0;
	P now = P(0,0);
	for(int i = 0; i < 1000; i++){
		if(d[i].first > now.first && d[i].second > now.second){
			res++;
			now = d[i];
		}
	}
	return res;
}

void solve()
{
	P Rs[1000] = {},Hs[1000] = {};
	P in;
	int c = 0;

	for(int i = 0; i < n; i++,c++){
		cin >> in.first >> in.second;
		Rs[c] = in;
		swap(in.first, in.second);
		Hs[c] = in;
	}
	cin >> m;
	for(int j = 0; j < m; j++,c++){
		cin >> in.first >> in.second;
		Rs[c] = in;
		swap(in.first, in.second);
		Hs[c] = in;
	}
	sort(Rs,Rs+c);
	sort(Hs,Hs+c);

	int res = calc(Rs,c);
	res = max(res, calc(Hs,c));
	cout << res << endl;
}


int main(void)
{
	while(cin >> n, n){
		solve();
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
const int MAX_NM = 200;

struct M{
	int r, h;
};

bool ascending_M_r(const M& l, const M& r){
	return l.r < r.r;
}

bool ascending_M_h(const M& l, const M& r){
	return l.h < r.h;
}

int main(){
	int n, m, dp[MAX_NM];
	M a[MAX_NM];
	while(std::cin >> n, n){
		for(int i=0;i<n;i++){
			std::cin >> a[i].h >> a[i].r;
		}
		std::cin >> m;
		for(int i=0;i<m;i++){
			std::cin >> a[n+i].h >> a[n+i].r;
		}
	
		int r_res = 0, h_res = 0;//hの最長増加部分列とrの最長増加部分列
		std::sort(a, a+n+m, ascending_M_r);

		dp[0] = 1;
		for(int i=1;i<n+m;i++){
			dp[i] = 1;
			for(int j=0;j<i;j++){
				if(a[j].r < a[i].r && a[j].h < a[i].h){
					dp[i] = std::max(dp[i], dp[j]+1);
				}
			}
		}
		r_res = dp[n+m-1];

		std::sort(a, a+n+m, ascending_M_h);
		dp[0] = 1;
		for(int i=1;i<n+m;i++){
			dp[i] = 1;
			for(int j=0;j<i;j++){
				if(a[j].r < a[i].r && a[j].h < a[i].h){
					dp[i] = std::max(dp[i], dp[j]+1);
				}
			}
		}
		h_res = dp[n+m-1];

		printf("%d\n", std::max(h_res, r_res));
	}
}

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;
        }
    }
}

C++: #include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
#include <vector>
#include <limits>

constexpr auto INF = std::numeric_limits<int>::max();

using Pair = std::pair<int, int>;

int n, m;
Pair size[210];
int dp[210];

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

            size[i].second *= -1;
        }
        std::cin >> m;
        for(int i = 0; i < m; ++i) {
            std::cin >> size[n + i].first >> size[n + i].second;

            size[n + i].second *= -1;
        }

        std::sort(size, size + n + m);
        std::fill(dp, dp + 210, INF);

        int max = 0;
        for(int i = 0; i < n + m; ++i) {
            size[i].second *= -1;

            auto p = std::lower_bound(dp, dp + 210, size[i].second);

            *p = size[i].second;

            max = std::max<int>(max, p - dp);
        }

        std::cout << max + 1 << std::endl;
    }
}