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++: # define _CRT_SECURE_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<" :"<<x<<endl;
# define EPS 1e-12
template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };

int n;
vi h, r;
int memo[2000][2000];
int dp(int H, int R)
{
	if (memo[H][R] != -1)return memo[H][R];
	int ans=0;
	for (int i = 0; i < n; i++)
	{
		if (h[i] > H&&r[i] > R)
		{
			ans = max(ans, dp(h[i], r[i]));
		}
	}
	ans++;
	return memo[H][R] = ans;
}
int main()
{
	while (cin >> n&&n)
	{
		memset(memo, -1, sizeof memo);
		h.clear();
		r.clear();
		FOR(i, n)
		{
			INT(H); INT(R);
			h.push_back(H);
			r.push_back(R);
		}
		INT(m);
		n += m;
		FOR(i, m)
		{
			INT(H); INT(R);
			h.push_back(H);
			r.push_back(R);
		}
		cout << dp(0, 0)-1 << endl;
	}
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

const int MAX_N = 100005;

//[1, n]
int bit[MAX_N + 1] = {0};

int sum(int i){
    int s = 0;
    while(i > 0){
        s = max(s,bit[i]);
        i -= i & -i;
    }
    return s;
}

void add(int i, int x, int n){
    while(i <= MAX_N){
        //bit[i] += x;
        bit[i] = max(bit[i], x);
        i += i & - i;
    }
}

int main(){
    int n,m;
    while(cin >> n,n){
        memset(bit,0,sizeof(bit));
        pair<int, int> p[MAX_N];
        rep(i,n) cin >> p[i].first >> p[i].second;
        cin >> m;
        range(i,n,n + m) cin >> p[i].first >> p[i].second;

        rep(i,n + m) p[i].second *= -1;
        sort(p, p + n + m);
        rep(i,n + m) p[i].second *= -1;

        int dp[MAX_N] = {0};
        rep(i,n + m){
            dp[i] = sum(p[i].second - 1) + 1;
            add(p[i].second, dp[i], n + m);
        }
        cout << *max_element(dp, dp + MAX_N) << endl;
    }
}

C++: #include<iostream>
using namespace std;
int main(){
long n,m,i,j,k,h[100],r[100],h2[100],r2[100],c,cc;
while(cin>>n){
if(n==0)break;
for(i=0;i<n;i++)cin>>h[i]>>r[i];
cin>>m;
for(i=0;i<m;i++)cin>>h2[i]>>r2[i];
k=c=0;
for(i=0;i<n;i++){
for(j=k;j<m;j++){
if(h2[j]<h[i]&&r2[j]<r[i]){
if(i==0||h[i-1]<h2[j]&&r[i-1]<r2[j]){
k=j+1;c++;
}
}
}
}
cc=n+c;
k=c=0;
for(i=0;i<m;i++){
for(j=k;j<n;j++){
if(h[j]<h2[i]&&r[j]<r2[i]){
if(i==0||h2[i-1]<h[j]&&r2[i-1]<r[j]){
k=j+1;c++;
}
}
}
}
if(cc>m+c)cout<<cc<<endl;else cout<<m+c<<endl;
}
return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define MAX_N 1000
int x[MAX_N], y[MAX_N], X[MAX_N][MAX_N], dist[MAX_N];
int A, B, N;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; } 
		for (int i = 0; i < A; i++) { cin >> x[i] >> y[i]; }
		cin >> B; 
		for (int i = A; i < A + B; i++) { cin >> x[i] >> y[i]; }N = A + B;
		for (int i = 0; i < N; i++) { 
			for (int j = 0; j < N; j++) { 
				if (x[i] < x[j] && y[i] < y[j]) { X[i][j] = 1; }
				else { X[i][j] = 0; }
			} 
		}
		queue<int>Q; int res = 0; for (int i = 0; i < N; i++) { dist[i] = 1; Q.push(i); }
		while (!Q.empty()) {
			int a1 = Q.front(); Q.pop();
			for (int i = 0; i < N; i++) {
				if (X[a1][i] == 1 && dist[a1] + 1 > dist[i]) { dist[i] = dist[a1] + 1; Q.push(i); }
			}
		}
		for (int i = 0; i < N; i++) { if (res < dist[i]) { res = dist[i]; } }cout << res << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define f first
#define s second
#define p(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)

int m,n,x,y;
vector<pair<int,int> >v;
int e[101][1001][1001];
int dfs(int i,int x,int y){
    if(i==v.size())return 0;
    if(e[i][x][y]!=-1){
        return e[i][x][y];
    }
    e[i][x][y]=max(dfs(i+1,x,y),(v[i].f>x&&v[i].s>y)?dfs(i+1,v[i].f,v[i].s)+1:0);
    return e[i][x][y];
}
int main(){
    while(cin>>n,n){
        g(0, 101)
        for(int j=0;j<1001;j++)
            for(int k=0;k<1001;k++)
                e[i][j][k]=-1;
        v.clear();
        g(0,n){
            cin>>x>>y;
            v.p(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.p(x, y);
        }
        sort(v.begin(),v.end());
        cout<<dfs(0,0,0)<<endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
P doll[200];
int memo[200];
int n,m;
int solve(int x) {
	if(memo[x]>-1) return memo[x];
	int sum=0;
	for(int i=x+1;i<(n+m);i++) {
		if(doll[x].second<doll[i].second)
			sum=max(sum,1+solve(i));
	}
	return memo[x]=sum;
}
int main() {
	while(scanf("%d",&n),n) {
		for(int i=0;i<n;i++) {
			int h,r;
			scanf("%d %d",&h,&r);
			doll[i].first=h,doll[i].second=r;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++) {
			int h,r;
			scanf("%d %d",&h,&r);
			doll[i+n].first=h,doll[i+n].second=r;
		}
		sort(doll,doll+n+m);
		memset(memo,-1,sizeof(memo));
		int sum=0;
		for(int i=0;i<(n+m);i++) {
			sum=max(sum,1+solve(i));
		}
		printf("%d\n",sum);
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

int n,m,h,r;

int main(){
	while(scanf("%d",&n),n != 0){
		vector<pair<int,int> > v = vector<pair<int,int> >(n);
		for(int i = 0; i < n; i++){
			scanf("%d%d",&h,&r);
			v[i] = make_pair(h,r);
		}
		scanf("%d",&m);
		for(int i = 0; i < m; i++){
			scanf("%d%d",&h,&r);
			v.push_back(make_pair(h,r));
		}
		sort(v.begin(),v.end());
		if(n + m == 0){
			printf("0\n");
			continue;
		}
		int ans = 1;
		pair<int,int> p = v[0];
		for(int i = 1; i < n + m; i++){
			if(p.first < v[i].first && p.second < v[i].second){
				p = v[i];
				ans++;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}