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>
#include <vector>

using namespace std;

#define H first
#define R second

typedef pair<int, int> pii;

int main ( void )
{
	int n, m;
	
	while (cin >> n, n)
	{
		int h, r;
		vector<pii> iv, jv;
		
		for (int i = 0; i < n; ++i)
		{
			cin >> h >> r;
			iv.push_back(make_pair(h, r));
		}
		
		cin >> m;
		
		for (int i = 0; i < m; ++i)
		{
			cin >> h >> r;
			jv.push_back(make_pair(h, r));
		}
		
		for (int i = jv.size()-1; i >= 0; --i)
		{
			pii ip = iv[iv.size()-1], jp = jv[i];
			
			if (ip.H < jp.H && ip.R < jp.R) 
			{
				iv.push_back(jp);
				continue;
			}
			
			ip = iv[0];
			if (jp.H < ip.H && jp.R < ip.R)
			{
				iv.insert(iv.begin(), jp);
				continue;
			}
			
			for (int j = iv.size()-2; j >= 0; --j)
			{
				pii &f = iv[j], &b = iv[j+1]; // front, back
				
				if (f.H < jp.H && f.R < jp.R && jp.H < b.H && jp.R < b.R)
				{
					iv.insert(iv.begin()+(j+1), jp);
					break;
				}
				if (f.H <= jp.H && f.R <= jp.R && jp.H < b.H && jp.R < b.R)
				{
					f = jp;
					break;
				}
			}
		}

		cout << iv.size() << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    int n,m;
    while(cin>>n,n!=0){
        int h[200],r[200];
        rep(i,n)cin>>r[i]>>h[i];
        int m;cin>>m;
        rep(i,m)cin>>r[n+i]>>h[n+i];
        vector<int>ord(n+m);
        rep(i,n+m)ord[i]=i;
        sort(ord.begin(),ord.end(),[&](int x,int y){return pint(r[x],-h[x])<pint(r[y],-h[y]);});
        int dp[222];
        rep(i,222)dp[i]=inf;
        dp[0]=0;
        for(auto i:ord){
            int idx=lower_bound(dp,dp+222,h[i])-dp;
            dp[idx]=h[i];
        }
        int ans=lower_bound(dp,dp+222,inf)-dp;
        cout<<ans-1<<endl;
    }
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 29;
int n, m, p;
pair<int, int> doll[201];
int dp[2][201], dp2[201];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 201, INF);
		fill(dp[1], dp[1] + 201, INF);
		fill(dp2, dp2 + 201, INF);
		for (int i = 0; i < n; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			if (doll[i].first == doll[i - 1].first){
				doll[i].first = 0;
				doll[i].second = 0;
			}
		}
		for (int i = 0; i < n + m; i++){
			*lower_bound(dp[0], dp[0] + n + m, doll[i].second) = doll[i].second;
		}
		cout << lower_bound(dp[0], dp[0] + n + m, INF) - dp[0] << endl;
	}
}

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

using namespace std;

int main() {
	int n;
	while ( cin >> n, n ) {
		vector<int> h(n), r(n);
		for (int i = 0; i < n; ++i) {
			cin >> h[i] >> r[i];
		}
		int M; cin >> M;
		h.resize(n+M);
		r.resize(n+M);
		for (int i = 0; i < M; ++i) {
			cin >> h[n+i] >> r[n+i];
		}
		n += M;

		int m[200][200] = {0};
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (h[i] < h[j] && r[i] < r[j]) {
					m[i][j] = -1;
				}
			}
		}
		int ans = 0;
		for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
			if (m[i][k] && m[k][j]) {
				m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
			}
			ans = min(ans, m[i][j]);
		}
		cout << (-ans + 1) << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int n, m, h[200], r[200];
int rec( int idx){
  int ret = 0;
  for(int i = 0; i < n + m; i++){
    if(h[idx] > h[i] && r[idx] > r[i]){
      ret = max( ret, rec(i) + 1);
    }
  }
  return ret;
}
int main(){
  while( cin >> n, n){
    for(int i = 0; i < n; i++){
      cin >> h[i] >> r[i];
    }
    cin >> m;
    for(int i = 0; i < m; i++){
      cin >> h[n + i] >> r[n + i];
    }
    h[n + m] = 1000, r[n + m] = 1000;
    cout << rec( n + m ) << endl;
  }
}

C++: #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define ALL(A)		(A).begin(),(A).end()
#define DUMP(A)    cout<<#A<<"="<<(A)<< endl
#define SIZE(A)    (int)((A).size())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main(){
    int N,M;
    for(;;){
        scanf("%d",&N);
        vector<P> mats;
        if(N==0) break;
        int h,r;
        for(int i=0;i<N;i++){
            scanf("%d%d",&h,&r);
            mats.push_back(P(h,-r));
        }
        scanf("%d",&M);
        for(int i=0;i<M;i++){
            scanf("%d%d",&h,&r);
            mats.push_back(P(h,-r));
        }
        sort(ALL(mats));
        vector<int> dp(N+M,INT_MAX);
        for(int i=0;i<N+M;i++){
            int now=-mats[i].second;
            *lower_bound(ALL(dp),now)=now;
        }
        printf("%d\n",distance(dp.begin(),lower_bound(ALL(dp),INT_MAX)));
    }
}