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>

using namespace std;

int main(){/*main*/
  int i,j,k,n,h[201],hn2,hen,r[201],m,hn,count,ans[201],min,max;

  while(1)
    {/*while*/
      max=0;
      hen=0;
      count=0;
      h[0]=-1;
      r[0]=-1;
      for(i=0;i<201;i++)
    {
      ans[i]=0;
    }
      ans[0]=0;

      cin >> n ;
      if(n==0)break;
      for(i=1;i<=n;i++)
    {
      cin >> h[i] >> r[i];
    }

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

      /*
      for(i=1;i<(n+m);i++)
      {
      printf("TES_h%d %d  TES_r%d %d\n",i,h[i],i,r[i]);
      }
      */
     


      for(i=1;i<=(m+n);i++)
    {/*for1*/
      min=i;
      for(j=i;j<=(m+n);j++)
        {/*for2*/
          if(h[j]<h[min])
        {
          min=j;
        }
        }/*E_for2*/
      hn=h[min];
      h[min]=h[i];
      h[i]=hn;

      hn2=r[min];
      r[min]=r[i];
      r[i]=hn2;
    }/*E_for1*/

 
      //    for(i=1;i<=(n+m);i++)
      // {
      //	printf("TES_h[%d] %d  TES_r[%d] %d\n",i,h[i],i,r[i]);
      // }
     

       /*   for(i=1;i<=(m+n);i++)
    {
      if(h[i]>h[i-1] && r[i]>r[i-1])
        {
          count+=1;
          ans[i]=1;
        }
      else
        {
          ans[i-1]=0;
          for(j=1;j<=i;j++)
        {
          if((h[i]>h[j] && r[i]>r[j]) && ans[j]==0 &&(h[i]!=h[i-1] && r[i]!=r[i-1]))
            {
              hen++;
            }

        }
          if(hen>0)count++;
        }
*/


       for(i=1;i<=(m+n);i++)
	 {
	   k=0;
	   for(j=1;j<i;j++)
	     {
	       if(r[i]>r[j] && j>k && ans[j]>ans[k])
		 {
		   k=j;
		 }
	       // cout << "TES_max"<< max << endl;
	     }
	   ans[i]=ans[k]+1;
	 }
       
       max=-1;
       for(i=1; i<=n+m; i++)
	 {
	   if(max<ans[i])
	     {
	       max=ans[i];
	     }
	 }

   
       
       
       cout << max << endl;
       
       
    }/*E_while*/
  return 0;
}/*E_main*/

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);
        }

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

        cout << endl;

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

        int dp[1001] = {0};

        for(int i=0;i<n+m;i++){
            int dm = 0;
            for(int j=0;j<v[i].r;j++){
                dm = max(dm,dp[j]);
            }
            dp[v[i].r] = dm+1;
        }

        int ans = 0;

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

        cout << ans << endl;


    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, h, r;
vector<P> p1, p2;
bool used[110];

int main(){
	while (cin >> n&&n){
		fill(used, used + 110, 0);
		REP(i, n){
			cin >> h >> r;
			p1.push_back(P(h, r));
		}
		p1.push_back(P(INF, INF));
		cin >> m;
		REP(i, m){
			cin >> h >> r;
			p2.push_back(P(h, r));
		}
		sort(ALL(p1));
		sort(ALL(p2));
		int ans = n;
		int nowh = -1, nowr = -1;
		REP(i, p1.size()){
			REP(j, m){
				//cout << used[i] << " ";
				if (used[j] == 0){
					if (nowh < p2[j].first&&p2[j].first < p1[i].first&&
						nowr < p2[j].second&&p2[j].second < p1[i].second){
						ans++;
					}
					else if (p1[i].first <= p2[j].first || p1[i].second <= p2[j].second){
						nowh = p1[i].first, nowr = p1[i].second;
						break;
					}
					used[j] = 1;
				}
			}
			//cout << endl;
		}
		if (ans < max(m, n))cout << max(m, n) << endl;
		else cout << ans << endl;
		p1.clear();
		p2.clear();
	}
}

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

using namespace std;
typedef pair<int, int> P;

void solve()
{
	int n;
	while(cin >> n, n)
	{
		map<int, int> data;
		for(int i = 0; i < n; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; ++i)
		{
			int h, r;
			cin >> h >> r;
			data.insert(map<int, int>::value_type(h, r));
		}
		vector<P> doll;
		/*map<int, int>::reverse_iterator rit = data.rbegin();
		while(rit != data.rend())
		{
			doll.push_back(P(rit->first, rit->second));
			++rit;
		}*/
		map<int, int>::iterator it = data.begin();
		while(it != data.end())
		{
			doll.push_back(P(it->first, it->second));
			++it;
		}
		int max_doll_count = 0;
		for(int i = 0; i < doll.size(); ++i)
		{
			int doll_count = 1;
			P current = doll[i];
			for(int j = i + 1; j < doll.size(); ++j)
			{
				if(current.first < doll[j].first && current.second < doll[j].second)
				{
					++doll_count;
					current = doll[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}
		
		/*vector<P> doll2;
		map<int, int>::reverse_iterator rit2 = data.rbegin();
		while(rit2 != data.rend())
		{
			doll2.push_back(P(rit2->second, rit2->first));
			++rit2;
		}
		for(int i = 0; i < doll2.size(); ++i)
		{
			int doll_count = 1;
			P current = doll2[i];
			for(int j = i + 1; j < doll2.size(); ++j)
			{
				if(current.first > doll2[j].first && current.second > doll2[j].second)
				{
					++doll_count;
					current = doll2[j];
				}
			}
			if(max_doll_count < doll_count)
			{
				max_doll_count = doll_count;
			}
		}*/
		cout << max_doll_count << endl;
	}
}

int main()
{
	solve();
	return(0);
}