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<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
P mt[200];
int dp[1000][201];//hでn個のときのrの最小値
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
int main(){
	int n,m;
	int i,j,k;
	while(1){
	scanf("%d",&n);
	if(n==0)return 0;
	for(i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		mt[i]=P(a,b);
	}
	scanf("%d",&m);
	for(i=0;i<m;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		mt[n+i]=P(a,b);
	}
	sort(mt,mt+m+n);
	for(i=0;i<1000;i++)for(j=0;j<=m+n;j++)dp[i][j]=1000;
	for(i=0;i<m+n;i++){
		int h=mt[i].first;
		int r=mt[i].second;
		dp[h][1]=min(dp[h][1],r);
		for(j=0;j<h;j++){
			for(k=0;k<m+n;k++){
				if(r>dp[j][k])dp[h][k+1]=min(dp[h][k+1],r);
			}
		}
	}
	int ans=0;
	for(i=0;i<1000;i++)for(j=0;j<=m+n;j++){
		if(dp[i][j]!=1000)ans=max(ans,j);
	}
	printf("%d\n",ans);
	}
}

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

typedef std::pair<int,int> P;

int dp[200];//何番目のまとりょーしかが一番大きいか＝数

int main(){
	int ichiro,jiro,M=0;
	while(true){
		P dool[201];//高さ　半径
		std::cin>>ichiro;
		if(!ichiro){
			break;
		}
		for(int i=0;i<ichiro;i++){
			std::cin>>dool[i].first>>dool[i].second;
		}
		std::cin>>jiro;
		for(int i=0;i<jiro;i++){
			std::cin>>dool[i+ichiro].first>>dool[i+ichiro].second;
		}
		std::sort(dool,dool+ichiro+jiro);
		for(int i=0;i<ichiro+jiro;i++)dp[i]=1;
		for(int i=1;i<ichiro+jiro;i++){
			for(int j=i-1;j>=0;j--){
				dp[i]=std::max(dp[i],dp[j]+(dool[i].second>dool[j].second));
			}
			M=std::max(M,dp[i]);
		}
		std::cout<<M<<std::endl;
		M=0;
		for(int i=0;i<ichiro+jiro;i++)dp[i]=0;
	}
	return 0;
}

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

int main() {
    int n, m, h, r;
    while (cin >> n, n) {
        vector<pair<int,int> > v;
        for (int i=0; i<n; ++i) {
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        cin >> m;
        for (int i=0; i<m; ++i) {
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        sort(v.begin(), v.end());
        int mx = 0, dp[100];
        for (int i=0; i<n+m; ++i) {
            dp[i] = 1;
            for (int j=0; j<i; ++j) {
                if (v[j].first < v[i].first && v[j].second < v[i].second) {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            mx = max(mx, dp[i]);
        }
        cout << mx << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
	EACH(x,vec) is >> x;
	return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
	for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
		if (i) os << " ";
		os << get<0>(t);
	}
	return os;
}
*/
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
	REP(i,vec.size()) {
		if (i) os << " ";
		os << vec[i];
	}
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
	REP(i,vec.size()) {
		if (i) os << endl;
		os << vec[i];
	}
	return os;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N;
	while ( cin >> N, N ) {
		vector<P> v;
		REP(i, N) {
			int h, r; cin >> h >> r;
			v.pb( P(h, r) );
		}
		int M; cin >> M;
		REP(i, M) {
			int h, r; cin >> h >> r;
			v.pb( P(h, r) );
		}
		sort( ALL(v) );
		N += M;
		vector< vector<int> > dp(N+1, vector<int>(N+1, -1)); dp[0][0] = 0;
		REP(i, N) {
			REP(j, N+1) {
				if (dp[i][j] < 0) continue;
				dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
				if (j == 0 || (v[j-1].first < v[i].first && v[j-1].second < v[i].second)) {
					dp[i+1][i+1] = max(dp[i+1][i+1], dp[i][j]+1);
				}
			}
		}
		int ans = -1;
		REP(i, N+1) ans = max(ans, dp[N][i]);
		cout << ans << endl;
	}
}

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

using namespace std;

#define fst first
#define snd second
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef pair<int,int> pii;

int n,m;
int dp[210];
pii dolls[210];

const int inf=1<<27;

int main(void){
	while(cin >> n ,n){
		rep(i,210) dp[i]=inf;

		rep(i,n) cin >> dolls[i].fst >> dolls[i].snd;
		cin >> m;
		rep(i,m) cin >> dolls[n+i].fst >> dolls[n+i].snd;
		
		sort(dolls,dolls+n+m);

		int prev=-1;
		rep(i,n+m){
			if(prev==dolls[i].fst)
				continue;
			*lower_bound(dp,dp+n+m,dolls[i].snd)=dolls[i].snd;
			prev=dolls[i].fst;
		}
		int ans=lower_bound(dp,dp+n+m,inf)-dp;
		cout << ans << endl;
	}
	return 0;
}