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<cstdio>
#define N 1000
using namespace std;

int r[N],h[N];

int syori(int,int);
void sort(int,int);

int main(){
  int n1,n2,i,ans;
 
  while(1){
 
    for(i=1;i<=N;i++){
      r[i]=0;
      h[i]=0;
    }


    r[0]=-1;
    h[0]=-1;
   
    cin >> n1;
    if(n1==0)break;
   
    for(i=1;i<=n1;i++){    // 1©çÍ¶ßÄ¢éÉÓ
      cin >> h[i] >> r[i];
    }
   
    cin >> n2;
   
    for(i=n1+1;i<=n1+n2;i++){ // P©çÍ¶ßÄ¢éÉÓ
      cin >> h[i] >> r[i];
    }
   
    sort(n1,n2);

    ans=syori(n1,n2);
   
    cout << ans << endl;
  }
 
 
}

void sort(int n1,int n2){
  int i,j,max,hi;

  for(i=0;i<=(n1+n2);i++){
    max=i;
    for(j=i;j<(n1+n2);j++){
      if(h[j] < h[max])max=j;
    }
      hi=h[max];
      h[max]=h[i];
      h[i]=hi;
   
         hi=r[max];
      r[max]=r[i];
      r[i]=hi;


  }

}



int syori(int n1,int n2){
  int ANS[N],i,j; //ANS[N]Í·³ð²×ézñ 
  int max=0,res=0;
 
  for(i=0;i<N;i++){
    ANS[i]=0;
    
  }
  
 
  for(i=1;i<=(n1+n2);i++){
    max=0;
    //  cout <<endl<< "h["<<i<<"]--"<<h[i]<<endl;    
    // cout << "r["<<i<<"]--"<<r[i]<<endl;
    // cout << "ANS["<<i<<"]--"<<ANS[i]<<endl;
    // cout<< "Lo["<<i<<"]--"<<Lo[i]<<endl<<endl;


    for(j=0;j<i;j++){
      if(h[j]<h[i] && r[j]<r[i] && ANS[j]>ANS[max]){
    max=j;   
      }
   
    }
 ANS[i]=ANS[max]+1;
      
  }

  //  for(i=1;i<=(n1+n2);i++){
  //  cout << "ANS[" << i << "]--" << ANS[i]<< endl;
  // }


  for(i=1;i<=(n1+n2);i++){
      if(res<=ANS[i]){
    res=ANS[i];
    }
  }
  return res;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={-1,0,1,0,1,1,-1,-1};
const int INF = 1<<30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector < Pii > data;
int N,M;
int memo[1280][1280];
int solve(int dep,int prev1,int prev2){
	if(dep == N+M){
		return 0;
	}
	if(memo[prev1][prev2])return memo[prev1][prev2];
	int ret = 0;
	reps(i,dep,N+M){
		if(prev1 < data[i].fi && prev2 < data[i].se){
			ret = max(ret , max(1 + solve(i+1,data[i].fi,data[i].se),solve(i+1,prev1,prev2)));
		}
		else{
			ret = max(ret,solve(i+1,prev1,prev2));
		}
	}
	return memo[prev1][prev2] = ret;
}
int main(){
	while(scanf("%d",&N),N){
		data.clear();
		memset(memo,0,sizeof(memo));
		rep(i,N){
			int a,b;
			scanf("%d%d",&a,&b);
			data.PB(Pii(a,b));
		}
		scanf("%d",&M);
		rep(i,M){
			int a,b;
			scanf("%d%d",&a,&b);
			data.PB(Pii(a,b));
		}
		sort(data.begin(),data.end());
		printf("%d\n",solve(0,0,0));
	}

    return 0;
}

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

struct D{int h, r;};
bool ascendDr(const D& d1, const D& d2){
  return d1.r < d2.r;
}
bool ascendDh(const D& d1, const D& d2){
  return d1.h < d2.h;
}

int main(void){
  int n, m, dp[200];
  D dolls[200];
  while(cin >> n, n){
    for(int i = 0; i < n; i++) cin >> dolls[i].h >> dolls[i].r;
    cin >> m;
    for(int i = n; i < n + m; i++) cin >> dolls[i].h >> dolls[i].r;

    int lis_r = 0, lis_h = 0;
    sort(dolls, dolls + n + m, ascendDr);

    fill(dp, dp + n + m, 0);
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h && dolls[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(dolls, dolls + n + m, ascendDh);

    fill(dp, dp + n + m, 0);
    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      for(int j = 0; j < i; j++){
        if(dolls[j].r < dolls[i].r && dolls[j].h && dolls[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 <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int main(){
  int n, m;
  int h, r;
  while(cin >> n, n){
    vector<P> v;
    for(int i = 0 ; i < n ; i++){
      cin >> h >> r;
      v.push_back(P(h, r));
    }
    cin >> m;
    for(int i = 0 ; i < m ; i++){
      cin >> h >> r;
      v.push_back(P(h, r));
    }
    sort(v.begin(), v.end());
    
    int dp[210];
    memset(dp, 0, sizeof(dp));
    
    int ans = -1;
    
    for(int i = 0 ; i < (int)v.size() ; i++){
      for(int j = 0 ; j < i ; j++){
	if(v[i].first > v[j].first && v[i].second > v[j].second)
	  dp[i] = max(dp[i], dp[j]+1);      
      }
      ans = max(ans, dp[i]);
    }
    
    cout << ans+1 << endl;
    
  }

  return 0;
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, dp[2000]; pair<int, int> a[2000];
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++;
		}
		printf("%d\n", ret);
	}
}

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

using namespace std;

int main() {
  
  int N;
  while(cin >> N && N) {
    
    vector<pair<int, int> > v1, v2;
    
    for(int i=0; i<N; i++) {
      int h, r; cin >> h >> r;
      v1.push_back(make_pair(h, r));
      v2.push_back(make_pair(r, h));
    }

    int M; cin >> M;
    
    for(int i=0; i<M; i++) {
      int h, r; cin >> h >> r;
      v1.push_back(make_pair(h, r));
      v2.push_back(make_pair(r, h));
    }
    
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    
    int size = N+M;
    int dp1[size];
    int ans = 0;
    for(int i=0; i<=size; i++) {
      dp1[i] = 1;
      for(int j=0; j<i; j++)
        if(v1[i].second > v1[j].second) { dp1[i] = max(dp1[i], dp1[j]+1); }
      ans = max(ans, dp1[i]);
    }
    
    int dp2[size];
    for(int i=0; i<=size; i++) {
      dp2[i] = 1;
      for(int j=0; j<i; j++)
        if(v2[i].second > v2[j].second) { dp2[i] = max(dp2[i], dp2[j]+1); }
      ans = max(ans, dp2[i]);
    }
    
    cout << ans << endl;
    
  }
  
  return 0;
}