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 namespace std;
#define MAX_NM 202
 
struct D{int r, h;};
 
bool compr(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool comph(const D& d1, const D& d2){
  return d1.h < d2.h;
}
 
int main(void){
  int n, m, dp[MAX_NM];
  D d[MAX_NM];
 
  while(cin >> n, n){
    for(int i = 0; i < n; i++) cin >> d[i].h >> d[i].r;
    cin >> m;
    for(int i = n; i < n + m; i++) cin >> d[i].h >> d[i].r;
 
    int lis_r = 0, lis_h = 0;
    sort(d, d + n + m, compr);
 
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(d[j].r < d[i].r && d[j].h < d[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];
 
    sort(d, d + n + m, comph);
 
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(d[j].r < d[i].r && d[j].h < d[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 <cstring>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> doll;

bool cmpr(const doll &a, const doll &b)
{
 	if( a.first == b.first )
		return a.second < b.second;
	else
		return a.first < b.first;
}

int main()
{
	int n, m, i, j;
	vector<doll> vc;
	int dp[200];
	
	vc.reserve(200);
	
	while( cin >> n, n)
	{
		vc.clear();
		memset( dp, 0, sizeof(dp));
		
		for( i = 0; i < n; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		cin >> m;
		
		for( i = 0; i < m; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		sort(vc.begin(), vc.end(), cmpr);
		
		dp[0] = 1;

		for( i = 1; i < n+m; i++)
		{
			dp[i] = 1;
			for( j = 0; j < i; j++)
			{
				if( vc[j].first < vc[i].first && vc[j].second < vc[i].second )
				{
					dp[i] = max( dp[j]+1, dp[i]);
				}
				else
				{
					dp[i] = max( dp[j], dp[i]);
				}
			}
			
//			cout << "dp::" << dp[i] << endl;
		}
		
		
		cout << dp[n+m-1] << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<cstdlib>
using namespace std;

struct ppp {
  int h;
  int r;
};

int tansaku(vector<ppp> c, vector<ppp> d, int cN, int dN, int ans, int cy, int dy) {
  cout << c[cN].h << " " << d[dN].h << " " << ans << " " << cN << " " << dN << endl;
  if(dN >= d.size()) {
    for(int i = cN; i < c.size(); i++) {
      cout << dy << endl;
      if(d[dy].r < c[i].r && d[dy].h < c[i].h) {
        ans++;
        d[dy] = c[i];
      }
    }
    return ans;
  }
  if(cN >= c.size()) {
    for(int i = dN; i < d.size(); i++) {
      if(d[i].r > c[cy].r && d[i].h > c[cy].h) {
        ans++;
        c[cy] = d[i];
      }
    }
    return ans;
  }
  if(c[cN].h == d[dN].h) {
    return c[cN].r > d[dN].r ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].r == d[dN].r) {
    return c[cN].h > d[dN].h ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].h < d[dN].h && c[cN].r < d[dN].r) {
    return tansaku(c, d, cN + 1, dN, ans + 1, cN, dy);
  }
  if(c[cN].h > d[dN].h && c[cN].r > d[dN].r) {
    return tansaku(c, d, cN, dN + 1, ans + 1, cy, dN);
  }
  if((c[cN].h < d[dN].h && c[cN].r > d[dN].r) || (c[cN].h > d[dN].h && c[cN].r < d[dN].r)) {
    return max(tansaku(c, d, cN + 1, dN, ans + 1, cN, dy), tansaku(c, d, cN, dN + 1, ans + 1, cy, dN));
  }
  return ans;
}

int main() {
  int a, b;
  cin >> a;
  vector<ppp> c(a);
  for(int i = 0; i < a; i++) {
    cin >> c[i].h >> c[i].r;
  }
  cin >> b;
  vector<ppp> d(b);
  for(int i = 0; i < b; i++) {
    cin >> d[i].h >> d[i].r;
  }
  cout << tansaku(c, d, 0, 0, 0, 0, 0) << endl;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,m;
  while(true){
      cin>>n;
      if(n==0) break;
      std::vector<P> v;
      for(int i=0;i<n;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      cin>>m;
      for(int i=0;i<m;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      v.push_back(P(0,0));
      sort(v.begin(),v.end());
      ll dp[n+m+1]={};
      for(int i=1;i<=n+m;i++){
          for(int j=0;j<i;j++){
              int h1=v[i].first,r1=v[i].second,h2=v[j].first,r2=v[j].second;
              if(h2<h1&&r2<r1){
                  dp[i]=max(dp[i],dp[j]+1);
              }
          }
      }
      ll res=-1;
      for(int i=1;i<=n+m;i++)res=max(res,dp[i]);
          cout<<res<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

vector<P> d;
int dp[200];

int cmp(P &a, P &b)
{
	if (a.first < b.first && a.second < b.second) return 1;
	return 0;
}

void solve()
{
	int n = d.size();
	fill(dp, dp + n, 1);
	for (int i = 1; i < n; i++){
		for (int j = 0; j < i; j++){
			if (cmp(d[j], d[i])){
				dp[i] = max(dp[i], dp[j] + 1);
			}
		}
	}
	int res = 0;
	for (int i = 0; i < n; i++){
		res = max(res, dp[i]);
	}
	printf("%d\n", res);
}

int main()
{
	while (true){
		d.clear();
		for (int c = 0; c < 2; c++){
			int n;
			scanf("%d", &n);
			if (!n) return 0;
			for (int i = 0; i < n; i++){
				int a, b;
				scanf("%d %d", &a, &b);
				d.push_back(P(a, b));
			}
		}
		sort(d.begin(), d.end());
		solve();
	}
}

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

using namespace std;

vector< pair<int,int> > doll;
int dp[200];
int n,m,tmp1,tmp2;

int main(){
  while(cin>>n && n){
    for(int i=0;i<n;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }

    //sort(doll.begin(),doll.end());

    int ans=0;

    for(int i=0;i<n+m;i++){
      dp[i]=1;
      for(int j=0;j<n+m;j++){
        if(doll[i].first>doll[j].first && doll[i].second>doll[j].second){
          dp[i]=max(dp[i],dp[j]+1);
        }
      }
      ans=max(ans,dp[i]);
    }

    cout<<ans<<endl;

    while(doll.size()) doll.pop_back();
    memset(dp,0,sizeof(dp));
  }
  return 0;
}