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,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

const int MAX_N=100,MAX_M=100,INF=1e8;

struct P{
	int h,r;
	P(){};
	P(int h,int r):h(h),r(r){};
	bool operator < (const P &a)const{
		if (h!=a.h){
			return h<a.h;
		}
		return r>a.r;
	}
};

int N,M;
P pa[MAX_N+MAX_M];

int dp[MAX_N+MAX_M+1];

int main(){
	do{
		scanf("%d",&N);
		if (N){
			int h,r;
			REP(i,N){
				scanf("%d %d",&h,&r);
				pa[i]=P(h,r);
			}
			scanf("%d",&M);
			REP(i,M){
				scanf("%d %d",&h,&r);
				pa[N+i]=P(h,r);
			}
			sort(pa,pa+N+M);
			fill(dp,dp+N+M+1,INF);
			REP(i,N+M){
				int ra=pa[i].r;
				*lower_bound(dp,dp+N+M+1,ra)=ra;
			}
			printf("%d\n",(int)(lower_bound(dp,dp+N+M+1,INF)-dp));
		}
	}while (N);
	return 0;
}

C++: #include<bits/stdc++.h>
#define mk make_pair
#define f first
#define s second
using namespace std;
int n,m,p1,p2,dp[201][1001][1001];
unordered_map<pair<int,pair<int,int> >,int >m1;

vector<pair<int,int> >v;

int dfs(int d,int h,int w){
  int res=0;
  if(m1[mk(d,mk(h,w))])return m1[mk(d,mk(h,w))];
  if(d==v.size()) return 1;
  if(v[d].f<h&&v[d].s<w)
    res=max(res,dfs(d+1,v[d].f,v[d].s))+1;
  res=max(res,dfs(d+1,h,w));
  return m1[mk(d,mk(h,w))]=res;
}

main(){
  while(cin>>n,n){
    m1.clear();
    v.clear();
    for(int i=0;i<n;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    for(int i=0;i<v.size();i++)swap(v[i].f,v[i].s);
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    cout<<dfs(0,1001,1001)-1<<endl;
  }
}

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

using namespace std;

const int MAX_SIZE = 1000;
const int N = 100;

main(){
  int n, m, sizeH, sizeR;
  int dp[MAX_SIZE+1][MAX_SIZE+1];
  int dataH[2][N];
  int dataR[2][N];
  while(cin >> n && n){
    sizeH = sizeR = 0;
    for(int i=0;i<n;++i){
      cin >> dataH[0][i] >> dataR[0][i];
      sizeH = max(sizeH, dataH[0][i]);
      sizeR = max(sizeR, dataR[0][i]);
    }
    cin >> m;
    for(int i=0;i<m;++i){
      cin >> dataH[1][i] >> dataR[1][i];
      sizeH = max(sizeH, dataH[1][i]);
      sizeR = max(sizeR, dataR[1][i]);
    }
    fill(dp[0], dp[MAX_SIZE], -1);
    dp[0][0] = 0;
    for(int i=0;i<sizeH;++i){
      for(int j=0;j<sizeR;++j){
	if(dp[i][j] == -1) continue;
	for(int k=0;k<n;++k){
	  if(dataH[0][k] > i && dataR[0][k] > j) 
	    dp[dataH[0][k]][dataR[0][k]] = max(dp[dataH[0][k]][dataR[0][k]], dp[i][j] + 1);
	}
	for(int k=0;k<m;++k){
	  if(dataH[1][k] > i && dataR[1][k] > j) 
	    dp[dataH[1][k]][dataR[1][k]] = max(dp[dataH[1][k]][dataR[1][k]], dp[i][j] + 1);
	}
      }
    }
    int ans = 0;
    for(int i=0;i<sizeH;++i){
      for(int j=0;j<sizeR;++j){
	ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> MAT;
#define h first
#define r second
#define rep(i,n) for(int i=0;i<n;i++)

int main(){
	int n,m;
	while(cin >> n , n ){
		vector<MAT> info(n);
		int ret = 0;
		rep(i,n){
			MAT t; cin >> t.h >> t.r ; 
			info.push_back(t);
		}
		cin >> m;
		rep(i,m){
			MAT t; cin >> t.h >> t.r ; 
			info.push_back(t);
		}
		n = n + m;
		
		int cnt = 1;
		/* ¼aî */
		sort(info.begin(),info.end(),greater<MAT>());
		
		MAT top = info[0];
		rep(i,n-1){
			if( info[i+1].first < top.first && info[i+1].second < top.second){
				top = info[i+1];
				cnt++;
			}
		}
		ret = max(ret,cnt);
		/* ³î */
		
		cnt = 1;
		rep(i,n)swap(info[i].first,info[i].second);
		sort(info.begin(),info.end(),greater<MAT>());
		
		top = info[0];
		
		rep(i,n-1){
			if( info[i+1].first < top.first && info[i+1].second < top.second){
				top = info[i+1];
				cnt++;
			}
		}
		ret = max(ret,cnt);
		
		cout << ret << endl;
	}
}

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

int N;

bool g[100][100];
int m[100][2];
vector<pair<pair<int, int>, int>> m1, m2;
vector<int> pa[100];

int dfs(int x)
{
  int r = 0;
  for(auto v: pa[x])
    r = max(r, dfs(v));
  return r + 1;
}

int main()
{
  while(true)
  {
    fill((bool*)g, (bool*)g + 10000, false);
    for(int i = 0; i < 100; i++)
      pa[i].clear(), (m[i][0] = m[i][1] = 0);
    m1.clear(); m2.clear();

    N = 0;
    for(int i = 0; i < 2; i++)
    {
      int a;
      cin >> a;
      if(!a)
        return 0;
      for(int j = N; j < a + N; j++)
      {
        cin >> m[j][0] >> m[j][1];
        m1.push_back(make_pair(make_pair(m[j][0], m[j][1]), j));
        m2.push_back(make_pair(make_pair(m[j][1], m[j][0]), j));
      } 
      N += a;
    }
    sort(m1.begin(), m1.end());
    sort(m2.begin(), m2.end());
    for(int i = 0; i < N; i++)
    {
      bool b1 = false, b2 = false;
      for(int j = i + 1; j < N; j++)
      {
        if(!b1 && m1[j].first.second > m1[i].first.second)
          pa[m1[i].second].push_back(m1[j].second), b1 = true;
        if(!b2 && m2[j].first.second > m2[i].first.second)
          pa[m2[i].second].push_back(m2[j].second), b1 = true;
      }
    }
    cout << max(dfs(m1[0].second), dfs(m2[0].second)) - 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;
    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;
            p[i].second *= -1;
        }
        int m;
        cin >> m;
        range(i,n,n + m){
            cin >> p[i].first >> p[i].second;
            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;
            add(p[i].second, dp[i], n + m);
        }
        cout << *max_element(dp, dp + MAX_N) << endl;
    }
}