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;

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 = 1;
    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;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ALL(T) T.begin(), T.end()
#define mp make_pair
#define pb push_back
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it = (c).begin(); it != (c).end(); it++)
#define H first
#define R second

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;
const int N = 201;

int dp[N];

int main(void){
    for(int n; cin >> n, n;){
        vector<pii> in(N);
        rep(i, n) cin >> in[i].H >> in[i].R;

        int m; cin >> m;
        rep(i, m) cin >> in[n + i].H >> in[n + i].R;

        rep(i, n + m)  dp[i] = 0;

        sort(in.begin(), in.begin() + n + m);

        rep(i, n + m) rep(j, i) if(in[j].H < in[i].H && in[j].R < in[i].R) dp[i] = max(dp[i], dp[j] + 1);

        cout << *max_element(dp, dp + m + n) + 1 << endl;
    }

    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

class mycmp{
public:
	bool operator()(const P p1, const P p2){
		if(p1.first < p2.first) return true;
		else if(p1.second < p2.second) return true;
		else false;
	}
};

int main(){
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}


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

		int* dp = new int[n+m];
		memset(dp,0,sizeof(dp));
		dp[0] = 1;

		for(int i=1;i<dolls.size();i++){
			for(int j=i-1;j>=0;j--){
				if(dolls[j].first < dolls[i].first
					&& dolls[j].second < dolls[i].second){
						dp[i] = max(dp[i],dp[j] + 1);
				}
				else dp[i] = dp[i-1];
			}
		}

		cout << dp[dolls.size()-1] << endl;
		delete[] dp;
	}
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define ALL(c) (c).begin(), (c).end() 
using namespace std;

struct doll {
	int h, r; // h, r <= 1,000
	doll() {}
	bool operator<(const doll& right) const {
		return h == right.h ? r < right.r : h < right.h;
	}
	bool bigger_than(const doll d) {
		return h > d.h && r > d.r;
	}
};

#define MAX 100

int n, m; // n, m <= 100
vector<doll> dolls;
int dp[MAX * 2]; // dp[i]: i??????????????¢?????????????????£????????????

int main()
{	
	int maxval, i;
/************** input **************/
	while(cin >> n, n) {
		for(i = 0; i < n; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
		cin >> m;
		for(i = 0; i < m; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
/************** solve **************/
		sort(ALL(dolls));
#if 0
		for(int i = 0; i < n + m; i++)
			cout << dolls.at(i).h << " " << dolls.at(i).r << endl;
#endif
		for(i = 0; i < n + m; i++) {
			maxval = 0;
			for(int j = i - 1; j >= 0; j--) 
				if(dolls.at(i).bigger_than(dolls.at(j)))
					maxval = max(maxval, dp[j]);
			dp[i] = maxval + 1;
		}
/************** output **************/
		maxval = 0;
		for(i = 0; i < n + m; i++)
			maxval = max(maxval, dp[i]);
		cout << maxval << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> MAT;
#define h first
#define r second
#define rep(i,n) for(int i=0;i<n;i++)

int main(){
	int n,m;
	while(cin >> n , n ){
		vector<MAT> info(n);
		int ret = 0;
		rep(i,n){
			MAT t; cin >> t.h >> t.r ; 
			info.push_back(t);
		}
		cin >> m;
		rep(i,m){
			MAT t; cin >> t.h >> t.r ; 
			info.push_back(t);
		}
		n = n + m;
		
		int cnt = 1;
		/* ¼aî */
		sort(info.begin(),info.end(),greater<MAT>());
		
		rep(st,n){
			MAT top = info[st];
			cnt = 1;
			for(int i = st + 1 ; i < n ; i++){
				if( info[i].first < top.first && info[i].second < top.second){
					top = info[i];
					cnt++;
				}
			}
			ret = max(ret,cnt);
		}
		/* ³î */
		
		cnt = 1;
		rep(i,n)swap(info[i].first,info[i].second);
		sort(info.begin(),info.end(),greater<MAT>());
		
		rep(st,n){
			MAT top = info[st];
			cnt = 1;
			for(int i = st + 1 ; i < n ; i++){
				if( info[i].first < top.first && info[i].second < top.second){
					top = info[i];
					cnt++;
				}
			}
			ret = max(ret,cnt);
		}
		
		cout << ret << endl;
	}
}