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<cstring>
#include<algorithm>
#include<vector>
#include<utility>
#define rep(i,a) for( int i = 0; i != (a); ++i )

const int INF = 1 << 30;

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

int n, m;
int dp[200];

int main()
{
	while( scanf( "%d", &n ), n )
	{
		memset( dp, 0, sizeof( dp ) );

		std::vector<P> ps;
		rep( i, n )
		{
			int h, r;
			scanf( "%d%d", &h, &r );
			ps.push_back( P( h, r ) );
		}

		scanf( "%d", &m );
		rep( i, m )
		{
			int h, r;
			scanf( "%d%d", &h, &r );
			ps.push_back( P( h, r ) );
		}

		std::sort( ps.begin(), ps.end() );

		int ans = 0;
		rep( i, ps.size() )
		{
			dp[i] = 1;
			
			rep( j, i )
				if( ps[i].first > ps[j].first && ps[i].second > ps[j].second )
					dp[i] = std::max( dp[i], dp[j]+1 );

			ans = std::max( ans, dp[i] );
		}

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, dp[200]; pair<int, int> a[200];
int main() {
	while(true) {
		scanf("%d", &n);
		if(n == 0) break;
		for(int i = 0; i < n; i++) scanf("%d%d", &a[i].first, &a[i].second);
		scanf("%d", &m);
		for(int i = 0; i < m; i++) scanf("%d%d", &a[n + i].first, &a[n + i].second);
		sort(a, a + n + m);
		int ret = 0;
		for(int i = 0; i < n + m; i++) dp[i] = -1;
		for(int i = 0; i < n + m; i++) {
			bool flag = false;
			for(int j = 0; j < ret; j++) {
				if(dp[j] >= a[i].second) {
					dp[j] = a[i].second; flag = true; break;
				}
			}
			if(!flag) {
				dp[ret] = a[i].second, ret++;
				int ptr = i;
				while(a[ptr].first == a[i].first) i++; i--;
			}
		}
		//for(int i = 0; i < n + m; i++) printf("%d %d\n", a[i].first, a[i].second);
		//for(int i = 0; i < ret; i++) printf("%d\n", dp[i]);
		printf("%d\n", ret);
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
 
int dp[1010][1010];
int n, m, h, r;
 
signed main(){
    while (cin >> n && n) {
        rep(i, 0, 1010) {
            rep(j, 0, 1010) {
                dp[i][j] = 0;
            }
        }
        vector<pii> doll;
        rep(i, 0, n) {
            cin >> h >> r;
            doll.push_back(make_pair(h, r));
        }
        cin >> m;
        rep(i, 0, m) {
            cin >> h >> r;
            doll.push_back(make_pair(h, r));
        }
        sort(all(doll));
        rep(i, 0, n + m + 1) {
            rep(j, 1, 1010) {
                if (doll[i].se == j) {
                    dp[i + 1][j] = dp[i][j - 1] + 1;
                }
                else {
                    dp[i + 1][j] = max(dp[i][j], dp[i + 1][j - 1]);
                }
            }
        }
        cout << dp[n + m + 1][1001] << endl;
    }
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 30;

int main(){
	int n, m, a, b;
	P doll[201];
	int dp[201];
	while (cin >> n && n){
		fill(dp, dp + 201, INF);
		for (int i = 0; i < n; i++){
			cin >> a >> b;
			doll[i] = P(a, b);
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			cin >> a >> b;
			doll[i] = P(a, b);
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			if (doll[i].first == doll[i - 1].first){
				doll[i] = P(0, 0);
			}
		}
		for (int i = 0; i < n + m; i++){
			*lower_bound(dp, dp + n + m, doll[i].second) = doll[i].second;
		}
		cout << lower_bound(dp, dp + n + m, INF) - dp << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define f first
#define s second
#define pb(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)
int main(){
    int n;
    while(cin>>n,n){
        vector<pair<int,int> >v;
        int m,x,y,x1,y1,x2,y2,a=1,c=1,b=101;
        g(0,n){
            cin>>x>>y;
            v.pb(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.pb(x, y);
        }
        x1=v[0].f,y1=v[0].s,x2=v[n].f,y2=v[n].s;
        sort(v.begin(),v.end());
        x=x1,y=y1;
        g(0,m+n){
            if(v[i].s>y&&v[i].f>x){
                a++;
                x=v[i].f,y=v[i].s;
            }
            else
                b=min(b,i);
        }
        x=x2,y=y2;
        g(0,m+n){
            if(v[i].s>y&&v[i].f>x){
                c++;
                x=v[i].f,y=v[i].s;
            }
        }
        cout<<max(a,c)<<endl;
    }
}

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

#define pb push_back
#define mk make_pair
typedef vector<pair<int, int> > vec;

int main(){
	int n;
	while( cin >> n, n ){
		vec doll;
		for( int i = 0; i < n; i++ ){
			int h, r;
			cin >> h >> r;
			doll.pb(mk(h, r));
		}
		int m;
		cin >> m;
		for( int i = 0; i < m; i++ ){
			int h, r;
			cin >> h >> r;
			doll.pb(mk(h, r));
		}
		sort(doll.begin(), doll.end());
		vector<int> dp(n+m, 1);
		int ans = 1;
		for( int i = 0; i < n+m; i++ ){
			for( int j = i; j < n+m; j++ ){
				if( doll[i].first < doll[j].first && doll[i].second < doll[j].second ){
					dp[j] = max(dp[j], dp[i]+1);
				}
				if( ans < dp[j] ) ans = dp[j];
			}
		}
		cout << ans << endl;
	}
}