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 <vector>
#include <algorithm>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main(){
  int n, m;
  int dp[201], dp2[201];
  P doll[200];
  while(cin >> n && n){
    fill(doll, doll+200, P(0, 0));
    fill(dp, dp + 200, INF);
    for (int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    dp[0] = doll[0].S;
    for (int i = 1; i < n + m; i++){
      int *p;
      p = lower_bound(dp, dp + 201, doll[i].S);
	if(doll[i].F != dp2[p-1-dp]){
	  *p = doll[i].S;
	  dp2[p-dp]=doll[i].F;
	}
    }
    cout << lower_bound(dp, dp + 201, INF) - dp << endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

vector<int> cont(const vector<Pii>& r,const vector<Pii>& l){
	vector<int> ret((int)r.size());
	int r_i = 0, l_i = 0;
	while(r_i < (int)r.size()){
		while(l_i < (int)l.size() &&
			(r[r_i].first >= l[l_i].first || r[r_i].second >= l[l_i].second)){
				l_i++;
		}
		ret[r_i] = l_i;
		r_i++;
	}
	return ret;
}

int memo[1000][2];

vector<int> v[2];
int func(int val,int a){
	if(memo[val][a] != -1) return memo[val][a];
	if(val == sz(v[a])) return memo[val][a] = 0;
	return memo[val][a] = max(func(val+1,a),func(v[a][val],a^1)) + 1;
}

int main(){
	int n,m;
	while(cin>>n,n){
		memset(memo,-1,sizeof(memo));
		vector<Pii> a(n);
		FOR(i,n) cin>>a[i].first>>a[i].second;
		cin>>m;
		vector<Pii> b(m);
		FOR(i,m) cin>>b[i].first>>b[i].second;
		v[0] = cont(a,b), v[1] = cont(b,a);
		int ans = max(func(0,0),func(0,1));

		cout << ans << endl;
	}

	return 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 = 0; i < 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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct DOLL{
	int h;
	int r;
};

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r)
{
	int ans = 1;
	for (DOLL &d : d1){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, h, r);
			ans = max(ans, a);
		}
	}
	for (DOLL &d : d2){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, h, r);
			ans = max(ans, a);
		}
	}
	return ans;
}

int main() {

	while (true){
		int n, m;
		cin >> n >> m;
		if (n == 0) break;

		vector<DOLL> d1;
		vector<DOLL> d2;

		for (int i = 0; i<n; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d1.push_back(d);
		}

		for (int i = 0; i<m; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d2.push_back(d);
		}

		int ans = f(d1, d2, 0, 0);
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int>P;
int n,m,ans;
int cnt;
P t[200];
int dp[200];
int main(){
  while(cin>>n&&n){
    cnt=ans=0;
    for(int i=0;i<n;i++)cin>>t[cnt].first>>t[cnt].second,cnt++;
    cin>>m;
    for(int i=0;i<m;i++)cin>>t[cnt].first>>t[cnt].second,cnt++;
    sort(t,t+cnt);
    for(int i=0;i<cnt;i++)dp[i]=1;

    for(int i=1;i<cnt;i++){
      
      for(int j=0;j<i;j++){
	if(t[j].first<t[i].first&&t[j].second<t[i].second){
	  dp[i]=max(dp[i],dp[j]+1);
	}
      }
      ans=max(ans,dp[i]);
      //cout<<t[i].first<<' '<<t[i].second<<' '<<dp[i]<<endl;
    }
    cout<<ans<<endl;
    
  }
  return 0;
}

C++: #include<iostream>

using namespace std;

const int MAX = 201;

typedef struct{
  int h;
  int r;
}DOLL;

int dp[MAX];
DOLL data[MAX];
int m,n;

void Dsort(){
  for(int i = 0; i < m+n-1; i++){
    for(int j = i; j < m+n; j++){
      if(data[i].r > data[j].r && data[i].h > data[j].h){
	DOLL tmp  = data[i];
	data[i] = data[j];
	data[j] = tmp;
      }
    }
  }
}

int solve(){
  dp[1] = 1;
  DOLL now;
  now.r = data[0].r;
  now.h = data[0].h;
  int out = 1;
  for(int i = 2; i < m+n; i++){
    if(data[i].h > now.h && data[i].r > now.r){
      dp[i] = dp[i-1]+1;
      now = data[i];
    }else dp[i] = dp[i-1];
    out = max(out,dp[i]);
  }
  return out+1;
}


int main(){

  while(cin >> n && n){
    for(int i = 0; i < n; i++) cin >> data[i].h >> data[i].r;
    cin >> m;
    for(int i = n; i < m+n; i++) cin >> data[i].h >> data[i].r;
    Dsort();
    cout << solve() << endl;
  }
  return 0;
}