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<iomanip>
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<deque>
#include<limits>
#include<list>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

int main() {
	for (int N; cin >> N, N;) {
		vector< pair<int, int> > v;
		for (int i = 0; i < N; i++) {
			int a, b; scanf("%d%d", &a, &b);
			v.push_back(make_pair(a, -b));
		}
		int M; cin >> M;
		for (int i = 0; i < M; i++) {
			int a, b; scanf("%d%d", &a, &b);
			v.push_back(make_pair(a, -b));
		}
		sort(v.begin(), v.end());
		for (int i = 0; i < N + M; i++) {
			v[i].second *= -1;
		}

		vector<int> dp(N + M, INF);
		for (int i = 0; i < N + M; i++) {
			*lower_bound(dp.begin(), dp.end(), v[i].second) = v[i].second;
		}
		int ans = find(dp.begin(), dp.end(), INF) - dp.begin();
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  int n,m;
  int i,j;
  int h,r;

  while(1){

    int doll[1005][1005]={};
    int dp[1005][1005]={};

    scanf("%d",&n);
    if(n==0)break;
    rep(i,n){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    int bj=1;

    for(i=1;i<1003;i++)
      for(j=bj;j<1003;j++){
	dp[i][j]=max(dp[i-1][j],dp[i][j-1])+doll[i][j];
	if(doll[i][j]){
	  for(int k=j+1;k<1003;k++)
	    dp[i][k]=dp[i][j];
	  bj=j+1;
	  break;
	}
      }

    cout<<dp[1000][1000]<<endl;
  }
}

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

const int MAX = 101;
typedef pair<int,int> P;

int main(){
    int n,m,h,r;
    
    while(cin >> n ,n){
        vector<P> v;
        int dp[MAX] = {0};
        
        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 res = 0;
        for(int i = 0 ; i < n+m ; i++){
            dp[i] = 1;
            for(int j = 0 ; j < i ; j++){
                if(v[j].first < v[i].first && v[j].second < v[i].second){
                    dp[i] = max(dp[i],dp[j]+1);
                }
                res = max(res,dp[i]);
            }
        }
        cout << res << endl;
    }
    
    return 0;
}

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

int main(){
	int n, m;
	while( cin >> n, n ){
		int doll[1001][1001] = {0};
		int dp[1001][1001] = {0};
		
		for(int i=0 ; i < n ; i++ ){
			int w, h;
			cin >> w >> h;
			doll[h][w] = 1;
		}
		cin >> m;
		for(int i=0 ; i < m ; i++ ){
			int w, h;
			cin >> w >> h;
			doll[h][w] = 1;
		}
		int ans = 0;
		for(int y=1 ; y < 1001 ; y++ ){
			for(int x=1 ; x < 1001 ; x++ ){
				if( doll[y-1][x-1] == 1 ){
					dp[y][x] = max( dp[y-1][x-1]+1 , max(dp[y-1][x],dp[y][x-1]) );
					ans = max( ans , dp[y][x] );
				}else{
					dp[y][x] = max(dp[y-1][x],dp[y][x-1]);
					ans = max( ans , dp[y][x] );
				}
			}
		}
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define PI acos(-1)

using namespace std;

struct Mato
{
	int x;
	int y;

	Mato (int x = 0,int y = 0) : x(x), y(y) {}
	bool operator < (const Mato& a) const {
		return x == a.x ? y < a.y : x < a.x;
	}
	
};

int main()
{
	int n,m;
	while(cin >> n && n)
	{
		vector<Mato> temp;
		int dp[1000];
		memset(dp,0,sizeof(dp));

		rep(i,n)
		{
			int h,r;
			cin >> h >> r;
			temp.push_back(Mato(h,r));
		}

		cin >> m;

		rep(i,m)
		{
			int h,r;
			cin >> h >> r;
			temp.push_back(Mato(h,r));
		}

		sort(temp.begin(),temp.end());
		
		int ans = 0;

		rep(i,temp.size())
		{
			dp[i] = 1;

			rep(j,i)
			{
				if(temp[j].x < temp[i].x && temp[j].y < temp[i].y)
				{
					dp[i] = max(dp[i],dp[j]+1);
				}
			}
			ans = max(ans,dp[i]);
		}

		cout <<ans << endl;

	}
	return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define MP(a, b) make_pair(a, b)
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n, m;
vector<pi>s;
int dp[1024];

int main(){
  while(scanf("%d", &n) && n){
    s.clear();
    int h, r;
    rep(i, n){
      scanf("%d%d", &h, &r);
      s.push_back(MP(h, r));
    }
    scanf("%d", &m);
    rep(i, m){
      scanf("%d%d", &h, &r);
      s.push_back(MP(h, r));
    }

    sort(s.begin(), s.end());
    memset(dp, 0, sizeof(dp));

    int res = 0;
    rep(i, s.size()){
      dp[i] = 1;
      rep(j, i) if(s[j].F < s[i].F && s[j].S < s[i].S){
    dp[i] = max(dp[i], dp[j] + 1);
      }
      res = max(res, dp[i]);
    }
    printf("%d\n", res);
  }
  return 0;
}

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

typedef pair<int,int> P;

int main(void){
	int n;

	while(1){
		vector<P> p;
		for(int i=0;i<2;i++){
			cin>>n;
			if(n == 0) return 0;

			for(int j=0;j<n;j++){
				int x,y;
				cin>>x>>y;
				p.push_back(P(x,y));
			}
		}

		n = p.size();
		sort(p.begin(),p.end());

		int res = 0;
		int dp[n];
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;i++){
			for(int j=0;j<i;j++){
				if(p[j].first < p[i].first &&
					 p[j].second < p[i].second &&
					 dp[i] < dp[j] + 1){
					dp[i] = dp[j] + 1;
				}
			}
			res = max(res,dp[i]);
		}

		cout<<res+1<<endl;
	}

	return 0;
}