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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<sstream>
#include<utility>
#include<map>
#include<vector>
#include<queue>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
vector<P>D;
int N[2],h,r,K,res,dp[205];
int main()
{
	for(;;)
	{
		res=0;
		D.clear();
		memset(dp,0,sizeof(dp));
		for(int c=0;c<2;c++)
		{
			scanf("%d",&N[c]);
			if(!c&&!N[0])return 0;
			for(int i=0;i<N[c];i++)
			{
				scanf("%d%d",&r,&h);
				D.push_back(make_pair(r,h));
			}
		}
		K=N[0]+N[1];
		sort(D.begin(),D.end());
		for(int i=0;i<K;i++)
		{
			dp[i]=max(dp[i],1);
			for(int j=0;j<i;j++)
				if(D[i].first>D[j].first&&D[i].second>D[j].second)
					dp[i]=max(dp[j]+1,dp[i]);
			res=max(dp[i],res);
		}
		printf("%d\n",res);
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>

using namespace std;

class doll{
public:

	doll(int a, int b){
		h = a;
		r = b;
	}

	int h;
	int r;

	bool operator<(doll arg){
		if(h < arg.h && r < arg.r){
			return true;
		}else{
			return false;
		}
	}
};

int main(void){
	int n;
	while(cin >> n && n){
		vector<doll> a,b,c;
		int h,r,ret;
		for(int i=0;i<n;i++){
			cin >> h >> r;
			a.push_back(doll(h,r));
		}
		cin >> n;
		for(int i=0;i<n;i++){
			cin >> h >> r;
			b.push_back(doll(h,r));
		}
		vector<doll>::iterator it,jt;
		bool isused[b.size()];
		memset(isused,false,sizeof(isused));
		ret = a.size();
		for(it=a.begin();it!=a.end();++it){
			jt = it;
			++jt;
			for(int pos=0;(unsigned int)pos<b.size();pos++){
				if(isused[pos]) continue;
				if(jt == a.end()){
					if(*it < b[pos]){
						isused[pos] = true;
						ret++;
					}
				}else{
					if(*it < b[pos] && b[pos] < *jt){
						isused[pos] = true;
						ret++;
					}
				}
			}
		}
		cout << ret << endl;
	}
}

C++: // AOJ 0157
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<sstream>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef pair<int,int> P;

const int NMAX = 150;

vector<P> ichiro, jiro;
int n, m;
int idp[NMAX], jdp[NMAX];
int index[NMAX], jndex[NMAX];

void init() {
  REP(i, NMAX) {
    idp[i] = jdp[i] = 0;
    index[i] = jndex[i] = 0;
  }
  jiro.clear();
  ichiro.clear();

  jiro.push_back(make_pair(0,0));
  ichiro.push_back(make_pair(0,0));
}

string str(P p) {
  stringstream ss;
  ss << "(" << p.first << "," << p.second << ")";
  return ss.str();
}

bool bigger(P big, P small) {
  return (big.first > small.first and big.second > small.second);
}

int lowerb(P mine, vector<P> his, int len) {
  for (int i = 1; i < len; i++) {
    bool f = bigger(mine,his[i]);
    if (not f) {
      return i - 1;
    }
  }
  return len - 1;
}

int rec(int i, int* mine, int* his) {
  if (i < 0) throw 1;
  if (i == 0) {
    return 0;
  } else {
    return 1 + max(rec(i - 1, mine, his), rec(mine[i], his, mine));
  }
}

int solve() {
  for (int i = 1; i <= n; i++) {
    index[i] = lowerb(ichiro[i], jiro, m);
  }

  for (int j = 1; j <= m; j++) {
    jndex[j] = lowerb(jiro[j], ichiro, n);
  }

  return max(rec(n, index, jndex), rec(m, jndex, index));
}

int main() {
  while (cin >> n and n) {
    init();
    REP(i, n) {
      int h, r;
      cin >> h >> r;
      ichiro.push_back(make_pair(h, r));
    }

    cin >> m;
    REP(i, m) {
      int h, r;
      cin >> h >> r;
      jiro.push_back(make_pair(h, r));
    }

    cout << solve() << endl;
  }
}

C++: #include<iostream>
#include<map>
#include<algorithm>
#include<cstring>
#define F first
#define S second
using namespace std;

int n, m, i;
pair < int, int > p[222];
int memo[222];

int solve(int k){
  
  if(memo[k]) return memo[k];
  
  int ret = 1;
  for(int i=0;i<n+m;i++){
    if(p[k].F > p[i].F && p[k].S > p[i].S) ret = max(ret, solve(i)+1);
  }

  return memo[k] = ret;
}

int main(){
  
  while(cin >> n, n){
    
    for(i=0;i<n;i++) cin >> p[i].F >> p[i].S;
    cin >> m;  
    for(;i<m+n;i++) cin >> p[i].F >> p[i].S;
    
    memset(memo, 0, sizeof(memo));
    
    int ans = 0;
    for(int j=0;j<n+m;j++) ans = max(solve(j), ans);

    cout << ans << endl;
  }
  
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
#define pint pair<int,int>
void dp(vector<vector<int> > v, int depth, int s, int &ret) {
	if(v[s].size() == 0) {
		if(ret < depth) ret = depth;
		return;
	}
	rep(i,v[s].size()) {
		dp(v,depth+1,v[s][i],ret);
	}
}
int main(){
	int n, m, x, y;
	while(cin >> n, n) {
		vector<pint> a;
		rep(i,n) {
			cin >> x >> y;
			a.push_back(make_pair(x, y));
		}
		cin >> m;
		rep(i,m) {
			cin >> x >> y;
			a.push_back(make_pair(x, y));
		}
		sort(a.begin(), a.end());
		vector<vector<int> > v(n+m);
		rep(i,n+m) {
			rep(j,n+m) {
				if(a[i].first < a[j].first && a[i].second < a[j].second) {
					v[i].push_back(j);
				}
			}
		}
		int ret = 0;
		rep(i,n+m) {
			dp(v,1,i,ret);
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdlib>
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 206
using namespace std;
int test=0;
int n,m;
int dp[MAX+1];
class Doll{
public:
   int h;
   int r;
   bool operator < (const Doll &d)const{
      if(h == d.h)return r < d.h;//hが同じ時ｒで比較
      return h < d.h;//普段はｈで比較
   }
};

Doll Dolls[MAX];

void solve(void){
   dp[0] = 0;
   //  rep(i,n+m){
   for(int i=0;i<(n+m);i++){
      dp[i+1] = 1;
      for(int j=0;j<i;j++)
	 if(Dolls[j].r < Dolls[i].r){
	    dp[i+1] = max(dp[i+1]+1,dp[j]+1);
      }
   }

/*
   cout << "test" << ++test << endl;
   rep(i,n+m)
      cout << dp[i] << endl;
   cout << "ans" << endl;
*/
   cout << dp[n+m] << endl;

}

int main(void){
   int h,r;
   while(1){
      cin >> n;
      if(n==0)break;
      rep(i,n){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      cin >> m;
      for(int i=n;i<(n+m);i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      sort(Dolls,Dolls+n+m);
      /*
      rep(i,n+m)
	 cout << Dolls[i].h << "  " << Dolls[i].r<<endl;
      */
      solve();

   }
   return 0;
}