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;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={-1,0,1,0,1,1,-1,-1};
const int INF = 1<<30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector < Pii > data;
int N,M;
int memo[2][1280][1280];
int solve(int dep,int prev1,int prev2){
	if(dep == N+M){
		return 0;
	}
	if(memo[dep%2][prev1][prev2])return memo[dep%2][prev1][prev2];
	int ret = 0;
	reps(i,dep,N+M){
		if(prev1 < data[i].fi && prev2 < data[i].se){
			ret = max(ret , max(1 + solve(i+1,data[i].fi,data[i].se),solve(i+1,prev1,prev2)));
		}
		else{
			ret = max(ret,solve(i+1,prev1,prev2));
		}
	}
	return memo[dep%2][prev1][prev2] = ret;
}
int main(){
	while(scanf("%d",&N),N){
		data.clear();
		memset(memo,0,sizeof(memo));
		rep(i,N){
			int a,b;
			scanf("%d%d",&a,&b);
			data.PB(Pii(a,b));
		}
		scanf("%d",&M);
		rep(i,M){
			int a,b;
			scanf("%d%d",&a,&b);
			data.PB(Pii(a,b));
		}
		sort(data.begin(),data.end());
		printf("%d\n",solve(0,0,0));
	}

    return 0;
}

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

typedef pair<int, int> doll;

bool cmpr(const doll &a, const doll &b)
{
 	if( a.first == b.first )
		return a.second < b.second;
	else
		return a.first < b.first;
}

int main()
{
	int n, m, i, j;
	vector<doll> vc;
	int dp[200];
	
	vc.reserve(200);
	
	while( cin >> n, n)
	{
		vc.clear();
		memset( dp, 0, sizeof(dp));
		
		for( i = 0; i < n; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		cin >> m;
		
		for( i = 0; i < m; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		sort(vc.begin(), vc.end(), cmpr);
		
		dp[0] = 1;
		int surface = 0;

		for( i = 1; i < n+m; i++)
		{
			
			if( vc[surface].first < vc[i].first && vc[surface].second < vc[i].second )
			{
				dp[i] = dp[surface] + 1;
				surface = i;
			}
			else
			{
				dp[i] = dp[surface];
			}
			
//			cout << "dp::" << dp[i] << endl;
		}
		
		
		cout << dp[n+m-1] << 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 = data[0];
  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];
  }
  return dp[m+n-1]+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;
}

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 = data[0];
  int out = 0;
  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;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    int n;
    while(cin>>n,n){
        vector<pii>v(1000,pii(0,0));
        rep(i,n){
            pii temp;
            cin>>temp.first>>temp.second;
            v[i]=temp;
        }
        int m; cin>>m;
        rep(i,m){
            pii temp;
            cin>>temp.first>>temp.second;
            v[n+i]=temp;
        }
        vi dp(10000,0);
        sort(v.begin(),v.begin()+n+m);
        int ans=0;
            rep(i,n+m){
                dp[i]=1;
                rep(j,i){
                    if(v[j].first<v[i].first&&v[j].second<v[i].second){
                        dp[i]=max(dp[i],dp[j]+1);
                    }
                    ans=max(ans,dp[i]);
                }
            }
        cout<<ans<<endl;
    }
    
}

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

int N, M, h[200], r[200];

int main() {
    while (true) {
        cin >> N;
        if (N == 0) break;
        for (int j = 0; j < N; ++j) {
            cin >> h[j] >> r[j];
        }
        cin >> M;
        for (int j = 0; j < M; ++j) {
            cin >> h[j+N] >> r[j+N];
        }

        int arr[201][201];
        for (int j = 0; j <= 200; ++j) fill(arr[j], arr[j] + 201, 0);

        for (int j = 0; j < N+M; ++j) {
            arr[h[j]][r[j]] = 1;
        }

        int dp[201][201];
        for (int j = 0; j <= 200; ++j) fill(dp[j], dp[j] + 201, 0);

        for (int j = 0; j <= 200; ++j) {
            for (int k = 0; k <= 200; ++k) {
                if (j > 0 && k > 0) {
                    dp[j][k] = max(dp[j-1][k-1] + arr[j][k], max(dp[j-1][k], dp[j][k-1]));
                } else if (j > 0) {
                    dp[j][k] = dp[j-1][k];
                } else if (k > 0) {
                    dp[j][k] = dp[j][k-1];
                }
            }
        }

        cout << dp[200][200] << endl;
    }
    return 0;
}