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 <vector>
#include <algorithm>
using namespace std;

const int INF = 1 << 30;

typedef struct
{
  int h, r;
} Doll;

int solve(int prev, vector<int>& used, const vector<Doll>& doll, vector<int>& dp)
{
  if (dp[prev] != -1)
    return dp[prev];

  int ret = 0;
  for (int i = 0; i < doll.size(); ++i) {
    if (!used[i] && doll[prev].h > doll[i].h && doll[prev].r > doll[i].r) {
      used[i] = 1;
      ret = max(ret, solve(i, used, doll, dp)+1);
      used[i] = 0;
    }
  }
  return dp[prev] = ret;
}

int main()
{
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    vector<Doll> doll;
    for (int i = 0; i < n; ++i) {
      int h, r;
      cin >> h >> r;
      doll.push_back((Doll){h, r});
    }

    int m;
    cin >> m;
    for (int i = 0; i < m; ++i) {
      int h, r;
      cin >> h >> r;
      doll.push_back((Doll){h, r});
    }

    doll.push_back((Doll){INF, INF});
    vector<int> used(n+m+1, 0);
    vector<int> dp(n+m+1, -1);
    cout << solve(n+m, used, doll, dp) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
//#include <tuple>
#include <set>
#include <string.h>
#include <functional>

#define X first
#define Y second
#define MP make_pair
//#define MT make_tuple
#define REP(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
#define FOR(i, n) REP(i, 0, n)
typedef long long ll;
using namespace std;
const int MAX = 101;

template<class T, class U>
void convert(T &t, U &u)
{
	stringstream ss;
	ss << t;
	ss >> u;
}

int dp[1001][1001];

int main()
{
	int n;
	while ( cin >> n && n ){
		memset(dp, 0, sizeof(dp));
		//入力.
		vector<pair<int, int> > data;
		FOR( i, n ){
			int s, t; cin >> s >> t;
			data.push_back( MP( s, t ) );
		}

		int m; cin >> m;
		FOR( i, m ){
			int s, t; cin >> s >> t;
			data.push_back( MP( s, t ) );
		}
		
		sort(data.begin(), data.end());


		dp[data[0].first][data[0].second] = 1;
		FOR(i, data.size()){
			REP(j, i + 1, data.size()){
				if (data[i].first < data[j].first && data[i].second < data[j].second){
					dp[data[j].first][data[j].second] = max(dp[data[j].first][data[j].second], dp[data[i].first][data[i].second] + 1);
				}
			}
		}

		int maxc = -1;
		FOR(i, data.size()){
			maxc = max(maxc, dp[data[i].first][data[i].second]);
		}

		cout << maxc << endl;
	}
	return 0;
}

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

int n,m;

typedef pair<int,int> P;

int calc(P d[1000],int c)
{
	int res = 0;
	P now = P(0,0);
	for(int i = 0; i < c; i++){
		if(d[i].first > now.first && d[i].second > now.second){
			res++;
			now = d[i];
		}
	}
	return res;
}

void solve()
{
	P Rs[1000],Hs[1000];
	P in;
	int c = 0;

	for(int i = 0; i < n; i++,c++){
		cin >> in.first >> in.second;
		Rs[c] = in;
		swap(in.first, in.second);
		Hs[c] = in;
	}
	cin >> m;
	for(int j = 0; j < m; j++,c++){
		cin >> in.first >> in.second;
		Rs[c] = in;
		swap(in.first, in.second);
		Hs[c] = in;
	}
	sort(Rs,Rs+c);
	sort(Hs,Hs+c);

	int res = calc(Rs,c);
	res = max(res, calc(Hs,c));
	cout << res << endl;
}


int main(void)
{
	while(cin >> n, n){
		solve();
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int, int>P;

P v1[200], v2[200];
bool used1[200], used2[200];
int main() {
	int n;
	while (scanf("%d", &n), n) {
		memset(used1, 0, sizeof(used1));
		memset(used2, 0, sizeof(used2));
		rep(i, n) {
			int h, r; scanf("%d%d", &h, &r);
			v1[i] = P(h, r);
		}
		int m; scanf("%d", &m);
		rep(i, m) {
			int h, r; scanf("%d%d", &h, &r);
			v2[i] = P(h, r);
		}
		sort(v1, v1 + n);
		sort(v2, v2 + m);
		v1[n] = v2[m] = P(2000, 2000);
		int cnt1 = 0;
		rep(i, n + 1) {
			rep(j, m) {
				if ((!i || (v1[i - 1].first < v2[j].first&&v1[i - 1].second < v2[j].second)) &&
					v2[j].first < v1[i].first&&v2[j].second < v1[i].second && !used1[j]) {
					used1[j] = true;
					cnt1++;
				}
			}
		}
		int cnt2 = 0;
		rep(i, m + 1) {
			rep(j, n) {
				if ((!i || (v2[i - 1].first < v1[j].first&&v2[i - 1].second < v1[j].second)) &&
					v1[j].first < v2[i].first&&v1[j].second < v2[i].second && !used2[j]) {
					used2[j] = true;
					cnt2++;
				}
			}
		}
		printf("%d\n", max(cnt1 + n, cnt2 + m));
	}
}

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

using namespace std;

typedef struct DOLL
{
	int h, r;
	
	bool operator > (const DOLL& d) const
	{
		return (h > d.h && r > d.r);
	}
	
	bool operator < (const DOLL& d) const
	{
		return (h < d.h && r < d.r);
	}
} D;

typedef struct Node
{
	D d;
	int num;
};

const int MAX = 100;

int n, m;
D A[MAX];
D B[MAX];

void solve()
{
	queue<Node> q;
	int max =0;
	Node n1 = {A[0], 1};
	Node n2 = {B[0], 1};
	q.push(n1);
	q.push(n2);
	while(!q.empty())
	{
		Node current = q.front();
		q.pop();
		
		if(max < current.num)
		{
			max = current.num;
		}
		
		for(int i=0; i<n; i++)
		{
			if(current.d < A[i])
			{
				Node new_node = {A[i], current.num+1};
				q.push(new_node);
				break;
			}
		}
		
		for(int i=0; i<m; i++)
		{
			if(current.d < B[i])
			{
				Node new_node = {B[i], current.num+1};
				q.push(new_node);
				break;
			}
		}
	}
	
	cout << max << endl;
}

int main(int argc, char** argv)
{
	int h, r;
	
	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 >> B[i].h >> B[i].r;
		}
		
		solve();
	}
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
	int n;
	while(cin>>n,n){
		vector<pii>in;
		while(n--){
			int a,b;
			cin>>a>>b;
			in.pb(pii(a,b));
		}
		cin>>n;
		while(n--){
			int a,b;
			cin>>a>>b;
			in.pb(pii(a,b));
		}
		sort(all(in));
		n=in.size();
		vi dp(110,1);
		loop(i,1,n){
			int ma=0;
			rep(j,i)if(in[j].first<in[i].first&&in[j].second<in[i].second)
				ma=max(ma,dp[j]);
			dp[i]+=ma;		
		}
		int ma=0;
		rep(i,n)ma=max(ma,dp[i]);
		cout<<ma<<endl;
	}
}