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 <memory>
#include <memory.h>
#include <cmath>
#include <fstream>
#include <cmath>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>
#define USE_MATH_DEFINES
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

int dp[300];
P ma[300];

int main(){
	int n, m;
	while(cin >> n && n){
		memset(dp, 0, sizeof(dp));
		rep(i, n) cin>> ma[i].first >> ma[i].second;
		cin >> m;
		rep(i, m) cin >> ma[n+i].first >> ma[n+i].second;
		sort(ma, ma + n + m);
		int res = 0;
		rep(i, n + m){
			int best = 0;
			rep(j, i){
				if(ma[i].first > ma[j].first && ma[i].second > ma[j].second) best = max(best, dp[j]);
			}
			dp[i] = best + 1;
			res = max(res, dp[i]);
		}
		cout << res << endl;
	}
}

C++: #include<iostream>

using namespace std;

int main(void){
  int n,m,h[201],r[201];

  while(cin >> n,n){
    for(int i=0;i<n;i++)cin >> h[i] >> r[i];
    cin >>m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(h[i]>h[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }
    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(r[i]>r[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }

    int dp[201]={0};
    dp[0]=1;
    for(int i=0;i<n+m;i++){
      for(int j=i+1;j<n+m;j++){
	if(h[i]<h[j] && r[i]<r[j] && dp[i]>=dp[j])dp[j]=dp[i]+1;
      }
    }
    int res=0;
    for(int i=0;i<n+m;i++)res=max(res,dp[i]);
    cout << res << endl;
  }

  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
struct S{int h,r,c;int operator<(S s){return h+r<s.h+s.r;}}a[201];
int main()
{
	int m,n,i,j;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<n;a[i++].c=0)scanf("%d%d",&a[i].h,&a[i].r);
		for(scanf("%d",&m),n+=m;i<n;a[i++].c=0)scanf("%d%d",&a[i].h,&a[i].r);
		a[n].h=a[n].r=a[n].c=0;
		std::sort(a,a+n+1);
		for(i=n;i>=0;--i)
			for(j=i+1;j<=n;++j)
				if(a[i].h<a[j].h&&a[i].r<a[j].r)
					a[i].c=std::max(a[i].c,a[j].c+1);
		printf("%d\n",a[0].c);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
const int MAX = 100000;
int dp[100];

int dfs(int index, vector<pair<int, int> > &mato){
	if(dp[index]) return dp[index];
	if(!index) return 1;
	int maxc = 0;
	for(int i = index-1; i >= 0; i--){
		if((mato[index].first<=mato[i].first)||(mato[index].second<=mato[i].second)) continue;
		maxc = max(maxc, dfs(i, mato));
	}
	dp[index] = maxc+1;
	return dp[index];
}

void substitute(int time, vector<pair<int, int> > &mato){
	for(int i = 0; i < time; i++){
		pair<int, int> tmp;
		cin >> tmp.first >> tmp.second;
		mato.push_back(tmp);
	}
}

int main(){
	int n, m;
	while(cin >> n && n != 0){
		fill(dp, dp+100, 0);
		vector<pair<int, int> > mato;
		substitute(n, mato);
		cin >> m;
		substitute(m, mato);
		sort(mato.begin(), mato.end());
		int maxc = 0;
		for(int i = n+m-1; i >= 0; i--) maxc = max(maxc, dfs(i, mato));
		cout << maxc << endl;
	}
	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
pii dolls[220];
int dp[220];//dp[i]:=i番目の人形を最後にしたときの最大の人形
int main(){
	int n,m;
	while(cin>>n,n){
		memset(dp, 0, sizeof(dp));
		memset(dolls, 0, sizeof(dolls));
		for(int i =0;i <n;i++){
			int h,r;
			cin>>h>>r;
			dolls[i].first=h;
			dolls[i].second=r;
		}
		cin>>m;
		for(int i =0;i <m;i++){
			int x,y;
			cin>>x>>y;
			dolls[n+i].first=x;
			dolls[n+i].second=y;
		}
		for(int i =0;i <n+m;i++){
			dp[i]=1;
		}
		sort(dolls,dolls+n+m);
		for(int i =0;i <n+m;i++){//最後の人形
			for(int j =0;j <n+m;j++){
				if(i==j)continue;
				if(dolls[i].first>dolls[j].first&&dolls[i].second>dolls[j].second){
					dp[i]=max(dp[i],dp[j]+1);
				}
			}
		}
		int ans = 0;
		for(int i =0;i <n+m;i++){
			ans=max(ans,dp[i]);
		}
		cout <<ans<<endl;
	}
	return 0;
}

C++: # define _CRT_SECURE_NO_WARNINGS
# include <cstdio>
# include <algorithm>

#define MAX 100

struct doll{
	int high, r;
	doll() {}
	doll(int H, int R) : high(H), r(R) {}
	bool operator<(doll d){
		return (high < d.high&&r < d.r);
	}
};

struct doll_c{
	doll d;
	int count;
};

int n, m;

doll K[MAX * 2];
doll_c dp[MAX * 2];

int main(){
	while (1){
		int h, r;
		scanf("%d", &n);
		if (n == 0)
			break;
		for (int i = 1; i <= n; ++i){
			scanf("%d %d", &h, &r);
			K[i].high = h; K[i].r = r;
		}
		scanf("%d", &m);
		for (int i = n + 1; i < m; ++i){
			scanf("%d %d", &h, &r);
			K[i].high = h;
			K[i].r = r;
		}
		std::sort(K + 1, K + 1 + n + m);
		dp[0].d = doll(-1, -1); dp[0].count = 0;
		for (int i = 1; i <= n + m; ++i){
			if (dp[i - 1].d < K[i]){
				dp[i].d = K[i];
				dp[i].count = dp[i - 1].count + 1;
			}
			else dp[i] = dp[i - 1];
		}
		printf("%d\n", dp[n + m].count);
	}
	return 0;
}