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 <cstring>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> doll;

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);
		}
		
		std::sort(vc.begin(), vc.end());
		
		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 <algorithm>
using namespace std;

int n,m;

typedef pair<int,int> p1;
typedef pair<p1,int> P;

int calc(P d[1000],int c)
{
	int res = 0;
	int ok = 0;
	P now = P(p1(0,0),0);
	for(int i = 0; i < 1000; i++){
		if(d[i].first.first > now.first.first && d[i].first.second > now.first.second){
			res++;
			now = d[i];
			ok |= d[i].second;
		}
	}

	if(ok != 3)
		return 0;
	return res;
}

void solve()
{
	P Rs[1000] = {},Hs[1000] = {};
	P in;
	int c = 0;

	for(int i = 0; i < n; i++,c++){
		cin >> in.first.first >> in.first.second;
		in.second = 1;
		Rs[c] = in;
		swap(in.first.first, in.first.second);
		Hs[c] = in;
	}
	cin >> m;
	for(int j = 0; j < m; j++,c++){
		in.second = 2;
		cin >> in.first.first >> in.first.second;
		Rs[c] = in;
		swap(in.first.first, in.first.second);
		Hs[c] = in;
	}
	sort(Rs,Rs+c);
	sort(Hs,Hs+c);

	int res = calc(Rs,c);
	res = max(res, calc(Hs,c));
	cout << res << endl;
}


int main(void)
{
	while(cin >> n, n){
		solve();
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fr first
#define sc second
typedef pair<int, int> Pii;

int main() {
  int n, m, a, b;
  while( cin >> n, n ) {
    vector<Pii> v;
    for(int i=0; i<n; i++) {
      cin >> a >> b;
      v.pb(Pii(a, b));
    }

    cin >> m;
    for(int i=0; i<m; i++) {
      cin >> a >> b;
      v.pb(Pii(a, b));
    }

    sort(v.begin(), v.end());
    vector<int> dp(n+m, 0);
    int res = 0;
    for(int i=0; i<n+m; i++) {
      int mx = 0;
      for(int j=0; j<i; j++) {
	if( v[j].fr < v[i].fr && v[j].sc < v[i].sc ) {
	  mx = max(mx, dp[j]);
	}
      }

      dp[i] = mx+1;
      res = max(res, dp[i]);
    }

    cout << res << endl;
  }
}

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

using namespace std;

const int MAX_SIZE = 1001;
const int N = 100;

main(){
  int n, m, sizeH, sizeR;
  int dp[MAX_SIZE+1][MAX_SIZE+1];
  int dataH[2][N];
  int dataR[2][N];
  while(cin >> n && n){
    sizeH = sizeR = 0;
    for(int i=0;i<n;++i){
      cin >> dataH[0][i] >> dataR[0][i];
      sizeH = max(sizeH, dataH[0][i]);
      sizeR = max(sizeR, dataR[0][i]);
    }
    cin >> m;
    for(int i=0;i<m;++i){
      cin >> dataH[1][i] >> dataR[1][i];
      sizeH = max(sizeH, dataH[1][i]);
      sizeR = max(sizeR, dataR[1][i]);
    }
    fill(dp[0], dp[MAX_SIZE], -1);
    dp[0][0] = 0;
    for(int i=0;i<=sizeH;++i){
      for(int j=0;j<=sizeR;++j){
	if(dp[i][j] == -1) continue;
	for(int k=0;k<n;++k){
	  if(dataH[0][k] > i && dataR[0][k] > j) 
	    dp[dataH[0][k]][dataR[0][k]] = max(dp[dataH[0][k]][dataR[0][k]], dp[i][j] + 1);
	}
	for(int k=0;k<m;++k){
	  if(dataH[1][k] > i && dataR[1][k] > j) 
	    dp[dataH[1][k]][dataR[1][k]] = max(dp[dataH[1][k]][dataR[1][k]], dp[i][j] + 1);
	}
      }
    }
    int ans = 0;
    for(int i=0;i<=sizeH;++i){
      for(int j=0;j<=sizeR;++j){
	ans = max(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main(){
  int n, m;
  int dp[2000], dp2[2000];
  P doll[2000];
  while(cin >> n && n){
    fill(doll, doll+2000, P(0, 0));
    fill(dp, dp + 2000, INF);
    fill(dp2, dp2 + 2000, 0);
    for (int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    dp[0] = doll[0].S;
    for (int i = 1; i < n + m; i++){
      if(doll[i].F!=doll[i-1].F)
	*lower_bound(dp, dp + 2000, doll[i].S) = doll[i].S;
    }
    cout << lower_bound(dp, dp + 2000, INF) - dp << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define INF doll(10000000,10000000)
using namespace std;
class doll{
public:
    int h,r;
    doll(int _h,int _r){
        h=_h;r=_r;
    }
    doll(){}
    bool operator<(const doll &d)const{
        return(h<d.h&&r<d.r);
    }
    bool operator==(const doll &d)const{
        return ((h<=d.h&&r>=d.r)||(h>=d.h&&r<=d.r));
    }

    bool operator>(const doll &d)const{
        return(h>d.h&&r>d.r);
    }
};
int main(){
    int n;
    while(cin>>n,n){
        queue<doll>N,M;
        for(int i=0;i<n;i++){
            int h,r;
            cin>>h>>r;
            N.push(doll(h,r));
        }
        int m;
        cin>>m;
        for(int i=0;i<m;i++){
            int h,r;
            cin>>h>>r;
            M.push(doll(h,r));
        }
        N.push(INF);M.push(INF);
        int cnt=0;
        while(true){
            if(N.front()<M.front())N.pop();
            else if(N.front()>M.front())M.pop();
            else{
                if(N.front()==INF)break;
                N.pop();M.pop();
            }
            cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}