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<algorithm>

using P = std::pair<int, int>;

int n;

P dolls[200];

int dp[200];

int main() {
	while (1) {
		int m;
		std::cin >> n;
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		std::cin >> m;
		for (int i = n; i < n + m; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		n += m;
		std::sort(dolls, dolls + n);
		dp[0] = 1;
		for (int i = 1; i < n; ++i) {
			dp[i] = dp[i - 1];
			for (int j = i - 1; j >= 0; --j) {
				if (dolls[i].first > dolls[j].first&&
					dolls[i].second > dolls[j].second) {
					dp[i] = std::max(dp[j] + 1, dp[i]);
					break;
				}
			}
		}
		std::cout << dp[n - 1] << std::endl;
	}
	return 0;
}

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

typedef std::pair<int,int> P;

int dp[200];//何番目のまとりょーしかが一番大きいか＝数

int main(){
	int ichiro,jiro,M=0;
	while(true){
		P dool[200];//高さ　半径
		std::cin>>ichiro;
		if(!ichiro){
			std::cout<<"0"<<std::endl;
			break;
		}
		for(int i=0;i<ichiro;i++){
			std::cin>>dool[i].first>>dool[i].second;
		}
		std::cin>>jiro;
		for(int i=0;i<jiro;i++){
			std::cin>>dool[i+ichiro].first>>dool[i+ichiro].second;
		}
		std::sort(dool,dool+ichiro+jiro);
		for(int i=0;i<ichiro+jiro;i++)dp[i]=1;
		for(int i=1;i<ichiro+jiro;i++){
			for(int j=i-1;j>=0;j--){
				dp[i]=std::max(dp[i],dp[j]+(dool[i].second>dool[j].second));
			}
			M=std::max(M,dp[i]);
		}
		std::cout<<M<<std::endl;
		M=0;
		for(int i=0;i<ichiro+jiro;i++)dp[i]=0;
	}
	return 0;
}

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

const int MAX_N = 201;
const int MAX_H = 1001;
const int MAX_R = MAX_H;
int N;
pair<int,int> E[MAX_N];
int T[MAX_H];

int solve() {
  int ans = 0;
  sort(E,E+N);
  fill(T,T+MAX_N,1);
  for(int i = 0; i < N; ++i) {
    for(int j = 0; j < i; ++j) {
      if(E[i].first > E[j].first && E[i].second > E[j].second) {
	T[i] = max(T[i], T[j] + 1);
	ans = max(ans, T[i]);
      }
    }
  }
  return ans;
}

int main() {
  while(cin >> N && N) {
    for(int i = 0; i < N; ++i) cin >> E[i].first >> E[i].second;
    int m;
    cin >> m;
    for(int i = 0; i < m; ++i) cin >> E[N+i].first >> E[N+i].second;
    N += m;
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <vector>
#include <algorithm>
#include <cstdio>
int x[100001],d[100001],c[100001];

int lis(int n){
	int i,j,I;
/*
	for(i=-1;I=0,++i<n;d[i]=I+1)
		for(j=i-1;~j;j--)
			if(x[j]<x[i]&&I<d[j])I=d[j];
*/
	// http://stackoverflow.com/questions/6129682/longest-increasing-subsequenceonlogn
	int sz = 1;
	c[1] = x[0];
	d[0] = 1;
	for(i=0;++i<n;){
		if(x[i] < c[1]){
			c[1] = x[i];
			d[i] = 1;
		}else if(x[i] > c[sz]){
			c[sz+1] = x[i];
			d[i] = sz+1;
			sz++;
		}else{
			int k = std::lower_bound(c,c+sz,x[i])-c;
			c[k] = x[i];
			d[i] = k;
		}
	}
	for(i=j=0;i<n;i++)
		if(d[i]>d[j])j=i;
	return d[j];
}

int n;
int main(){
	for(;;){
		n=0;
		std::vector<std::pair<int,int>>v;
		int a,b;
		for(int k=0;k<2;k++){
			int n0;
			if(scanf("%d",&n0),!n0)return 0;
			for(n+=n0;n0--;){
				scanf("%d%d",&a,&b);
				v.emplace_back(a,-b);
			}
		}
		std::sort(v.begin(),v.end());
		for(int i=0;i<n;i++)x[i]=-v[i].second;
		printf("%d\n",lis(n));
	}
}

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

struct D{int r, h;};

bool ascendDr(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool ascendDh(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[200];
  D dolls[200];
  while(cin >> n){
    if(n == 0) break;

    for(int i = 0; i < n; i++) cin >> dolls[i].h >> dolls[i].r;
    cin >> m;
    for(int i = 0; i < m; i++) cin >> dolls[n+i].h >> dolls[n+i].r;

    int lis_r = 0, lis_h = 0;
    sort(dolls, dolls + n + m, ascendDr);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h && dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(dolls, dolls + n + m, ascendDh);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h && dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_h = dp[n+m-1];

    cout << max(lis_h, lis_r) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
typedef pair<int, int> P;
 
void solve()
{
    int n;
    while(cin >> n, n)
    {
        vector<P> doll;
        for(int i = 0; i < n; ++i)
        {
            int h, r;
            cin >> h >> r;
            doll.push_back(P(h, r));
        }
        int m;
        cin >> m;
        for(int i = 0; i < m; ++i)
        {
            int h, r;
            cin >> h >> r;
            doll.push_back(P(h, r));
        }
        sort(doll.begin(), doll.end());
        int N = doll.size();
        vector<int> dp(N, 1);
        int ans = 1;
        for(int i = 0; i < N; ++i)
        {
            for(int j = 0; j < i; ++j)
            {
                if(doll[j].first < doll[i].first && doll[j].second < doll[i].second)
                {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            ans = max(ans, dp[i]);
        }
        cout << ans << endl;
    }
}
 
int main()
{
    solve();
    return(0);
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX_NM 202

struct D{
  int r, h;
};

bool ascending_M_r(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool ascending_M_h(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[MAX_NM];
  D a[MAX_NM];

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

    int lis_r = 0, lis_h = 0;
    sort(a, a + n + m, ascending_M_r);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h && a[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(a, a + n + m, ascending_M_h);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h && a[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_h = dp[n+m-1];

    printf("%d\n", max(lis_h, lis_r));
  }
  return 0;
}