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>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 1<<30

typedef pair<int,int> pii;

int dp[1000][1000];         // dp[i][j] : ³ i, ¼a j ÜÅÌÍÍÉ¨³Üé
int m,n;
int h1[100];
int r1[100];
int h2[100];
int r2[100];

int solve(int h, int r, int x, int y) { // x : êYÌðã©çÇ±ÜÅgÁ½©@y : ñY
  // printf("%d,%d  %d,%d ",h,r,x,y);
  if (dp[h][r]>=0) {
//    cout << dp[h][r] << endl;
    return dp[h][r];
  }
  //cout << endl;
  int res = 0;
  for (int i=x-1; i>=0; --i) {
    if (h>h1[i] && r>r1[i])
      res = max(res, solve(h1[i],r1[i],i,y));
  }
  for (int i=y-1; i>=0; --i) {
    if (h>h2[i] && r>r2[i])
      res = max(res, solve(h2[i],r2[i],x,i));
  }
  return dp[h][r] = res + 1;
}

int main() {
  while(cin >> n, n) {
    REP(i,n) 
      cin >> h1[i] >> r1[i];
    cin >> m;
    REP(i,m)
      cin >> h2[i] >> r2[i];
    memset(dp,-1,sizeof(dp));
    cout << max(solve(h1[n-1],r1[n-1], n-1, m), solve(h2[m-1],r2[m-1], n, m-1)) << endl;
  }
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
#define pint pair<int, int>
int main(){
	int n, m;
	while(cin >> n, n) {
		pint p[200];
		rep(i,n) {
			cin >> p[i].first >> p[i].second;
		}
		cin >> m;
		rep(i,m) {
			cin >> p[n+i].first >> p[n+i].second;
		}
		sort(p,p+m+n);
		int dp[200], ret = 0;
		rep(i,200) dp[i] = 1;
		rep2(i,1,m+n) {
			rep(j,i) {
				if(p[j].first < p[i].first && p[j].second < p[i].second) {
					dp[i] = max(dp[i], dp[j]+1);
				}
			}
			ret = max(ret,dp[i]);
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef pair<int, int> P;

const int MAX_N = 100;
const int MAX_M = 100;
int dp[MAX_N];
//int h[MAX_N * 2], r[MAX_N * 2];
vector<P> vec;
// ???????????????

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n;
	while(cin >> n && n != 0){
	cin >> n;
	
	for(int i = 0; i < n; ++i){
		int h, r;
		cin >> h >> r;
		vec.push_back(P(h, r));
		//cin >> h[i] >> r[i];
	}
	
	int m;
	cin >> m;
	
	for(int i = n; i < n + m; ++i){
		int h, r;
		cin >> h >> r;
		vec.push_back(P(h, r));
		//cin >> h[i] >> r[i];
	}
	
	sort(vec.begin(), vec.end());
	
	for(int i = 0; i < n + m; ++i){
		//cout << vec[i].first << " " << vec[i].second << endl;
		dp[i] = 1;
	}
	
	int ans = 0;
	for(int i = 0; i < n + m; ++i){
		for(int j = 0; j < n + m; ++j){
			if(vec[i].first > vec[j].first && vec[i].second > vec[j].second){
				dp[i] = max(dp[i], dp[j] + 1);
			}
			ans = max(ans, dp[i]);
		}
	}
	
	cout << ans << endl;
	}
}

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

struct doll{
    int h,r;
};

bool operator<(const doll& d1,const doll& d2){
    if(d1.h == d2.h){
        return d1.r < d2.r;
    }
    return d1.h < d2.h;
}

int main() {

    while(1){

        int n;

        cin >> n;

        if(n==0) break;

        int h,r;

        vector<doll> v;
        doll tmp;

        for(int i=0;i<n;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }

        int m;

        cin >> m;

        for(int i=0;i<m;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }

        /*for(int i=0;i<n+m;i++){
            cout << v[i].h << " " << v[i].r << endl;
        }*/

        sort(v.begin(),v.end());

        int table[201][201];

        for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                table[i][j] = 0;
            }
        }

        /*for(int i=0;i<n+m;i++){
            table[i][i] = 1;
        }*/

        for(int i=0;i<n+m;i++){
            for(int j=i;j<n+m;j++){
                if( (v[i].h<v[j].h && v[i].r < v[j].r) || (v[i].h > v[j].h && v[i].r > v[j].r) ){
                    table[i][j] = 1;
                    //table[j][i] = 1;
                }
            }
        }

        /*for(int i=0;i<n+m;i++){
            for(int j=0;j<n+m;j++){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/

        int dp[201][201] = {0};

        for(int i=0;i<=n+m;i++){
            dp[i][0] = 0;
            dp[0][i] = 1;
        }

        for(int i=1;i<=n+m;i++){
            for(int j=1;j<=n+m;j++){
                if(table[i-1][j-1]==1){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+1);
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
            }

        }

        /*for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/

        int ans = 0;

        for(int i=0;i<=n+m;i++){
            //cout << dp[i] << endl;
            ans = max(dp[i][n+m],ans);
        }

        cout << ans << endl;


    }

    return 0;
}

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

int main(){
  int n,m;
  int h[200],r[200],dp[200];

  while(1){
    cin >> n;
    if(!n)break;

    for(int i=0;i<n;i++)cin >> h[i] >> r[i];

    cin >> m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

    for(int i=0;i<n+m;i++)
      for(int j=i+1;j<n+m;j++)
	if(h[i]>h[j] || (h[i]==h[j] && r[i]>r[j])){
	  swap(h[i],h[j]);
	  swap(r[i],r[j]);
	}
   
    int ans = 0;
    for(int i=1;i<n+m;i++)dp[i] = 0;
    dp[0] = 1;
    for(int i=1;i<n+m;i++){
      for(int j=0;j<i;j++)
	if(h[i]>h[j] && r[i]>r[j])dp[i] = max(dp[i],dp[j]+1);
      ans = max(ans,dp[i]);
    }
    
    cout << ans << endl;
  }
}

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

typedef pair<int,int> P;

int main(){
	int n,m,h,r;
	vector<P> mat;
	vector<int> l;
	while(cin >> n,n){
		for(int i = 0;i < n;i++) {
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		cin >> m;
		for(int i = n;i < n + m;i++){
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		sort(mat.begin(),mat.end());
		for(int i = 0;i < n + m;i++){
			if(l.empty()) l.push_back(mat[i].second);
			else{
				if(l.back() < mat[i].second) l.push_back(mat[i].second);
				else l[lower_bound(l.begin(),l.end(),mat[i].second) - l.begin()] = mat[i].second;
			}
		}
		cout << l.size() << endl;
		l.clear();
		mat.clear();
	}
	return 0;
}