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<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

int main(){
	int n[2],c[2];
	while(cin>>n[0],n[0]){
		int p;
		int h[2][110],r[2][110],mx[2][110];
		rep(p,2){
			rep(i,n[p])mx[p][i]=0,cin>>h[p][i]>>r[p][i];
			if(!p)cin>>n[1];
		}
		mx[0][0]=(h[0][0]>h[1][0]&&r[0][0]>r[1][0])+1;
		mx[1][0]=(h[0][0]<h[1][0]&&r[0][0]<r[1][0])+1;
		c[0]=c[1]=p=0;
		while(c[0]<n[0]||c[1]<n[1]){
			int tmax=0,f;
			rep(k,2){
				rep(i,n[k]){
					f=h[k][i]<h[p][c[p]]&&r[k][i]<r[p][c[p]];
					if(f&&tmax<mx[k][i])tmax=mx[k][i];
					if(f&&mx[k][i]==0){
						p^=1;
						goto NEXT;
					}
				}
			}
			mx[p][c[p]++]=tmax+1;
			NEXT:;
		}
		cout<<max(mx[0][n[0]-1],mx[1][n[1]-1])<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<cstdio>
#define N 1000
using namespace std;
int r[N],h[N];

int syori(int,int);
void sort(int,int);

int main(){
  int n1,n2,i,ans;
  
  while(1){
  
    for(i=1;i<=N;i++){
      r[i]=0;
      h[i]=0;
    }


    r[0]=-1;
    h[0]=-1;
    
    cin >> n1; 
    if(n1==0)break;
    
    for(i=1;i<=n1;i++){    // 1ツつゥツづァツづ債つカツづ淞づ?つ「ツづゥツ篠鳴づ可陳債暗?
      cin >> h[i] >> r[i];
    }
    
    cin >> n2;
    
    for(i=n1+1;i<=n1+n2;i++){ // ツ１ツつゥツづァツづ債つカツづ淞づ?つ「ツづゥツ篠鳴づ可陳債暗?
      cin >> h[i] >> r[i];
    }
    
    sort(n1,n2);

    ans=syori(n1,n2);
    
    cout << ans << endl;
  }
  
  
}

void sort(int n1,int n2){
  int i,j,max,rr;

  for(i=1;i<=(n1+n2);i++){
    for(j=1;j<=(n1+n2);j++){
      if(h[j] > h[i]){
	
	rr=h[i];
	h[i]=h[j];
	h[j]=rr;
	
	rr=r[i];
	r[i]=r[j];
	r[j]=rr;
	
	
      }
    }
    
    
  }
  
}



int syori(int n1,int n2){
  int ANS[N],i,j; //ANS[N]ツづ債陳キツつウツづーツ陳イツづ猟づゥツ配ツ療ア 
  int max=0,res=0;
  
  for(i=0;i<N;i++){
    ANS[i]=0;
   
  }
 
  
  for(i=1;i<=(n1+n2);i++){
    max=0;
    //  cout <<endl<< "h["<<i<<"]--"<<h[i]<<endl;     
    // cout << "r["<<i<<"]--"<<r[i]<<endl;
    // cout << "ANS["<<i<<"]--"<<ANS[i]<<endl;
   


    for(j=0;j<i;j++){
      if(h[j]<h[i] && r[j]<r[i] && ANS[j]>ANS[max]){
	max=j;    
      }
    
    }
 ANS[i]=ANS[max]+1;
       
  }

  //  for(i=1;i<=(n1+n2);i++){
  //  cout << "ANS[" << i << "]--" << ANS[i]<< endl;
  // }


  for(i=1;i<=(n1+n2);i++){
      if(res<=ANS[i]){
	res=ANS[i];
    }
  }
  return res;
}

C++: #include<iostream>
#include<cstdlib>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 3000
using namespace std;
int test=0;
int n,m;
int dp[MAX+1];
class Doll{
public:
   int h;
   int r;
   bool operator < (const Doll &d)const{
      if(h == d.h)return r < d.h;//hが同じ時ｒで比較
      return h < d.h;//普段はｈで比較
   }
};

Doll Dolls[MAX];

void solve(void){
   dp[0] = 0;
   dp[1] = 1;
   for(int i=0;i<(n+m);i++){
      dp[i+1] = 1;
      for(int j=0;j<i;j++){
	 if(Dolls[j].r < Dolls[i].r){
	    dp[i+1] = max(dp[i+1],dp[j+1]+1);
	 }
      }
   }
   int ans=0;
   for(int i=1;i<=n+m;i++){
      ans = max(dp[i],ans);
   }
   cout << ans << endl;
}

int main(void){
   int h,r;
   while(1){
      cin >> n;
      if(n==0)break;
      for(int i=0;i<n;i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      cin >> m;
      for(int i=n;i<(n+m);i++){
	 cin >> h >> r;
	 Dolls[i].h = h;
	 Dolls[i].r = r;
      }
      sort(Dolls,Dolls+(n+m)-1);
      solve();
      /*
      rep(i,n+m)
	 cout << Dolls[i].h << " " << Dolls[i].r << " " << dp[i+1] << endl;
      cout << endl;
      */
   }
   return 0;
}

C++: #include <stdio.h>
#include <algorithm>
using namespace std;
int n, m, dp[200]; pair<int, int> a[200];
int main() {
	while(true) {
		scanf("%d", &n);
		if(n == 0) break;
		for(int i = 0; i < n; i++) scanf("%d%d", &a[i].first, &a[i].second);
		scanf("%d", &m);
		for(int i = 0; i < m; i++) scanf("%d%d", &a[n + i].first, &a[n + i].second);
		sort(a, a + n + m);
		int ret = 0;
		for(int i = 0; i < n + m; i++) dp[i] = -1;
		for(int i = 0; i < n + m; i++) {
			bool flag = false;
			for(int j = 0; j < ret; j++) {
				if(dp[j] >= a[i].second) {
					dp[j] = a[i].second; flag = true; break;
				}
			}
			if(!flag) {
				dp[ret] = a[i].second, ret++;
				int ptr = i;
				while(a[ptr].first == a[i].first) i++; i--;
			}
		}
		printf("%d\n", ret);
	}
}

C++: #include <iostream>
#include <stdio.h>

using namespace std;

int main() {
 int mh[200],mr[200],mc[200],c,n,m,i,j,k;
 while(true) {
 c=0;
 for (i=0;i<2;i++) {
	 cin >> n;
	 if (n==0) return 0;
	 for (j=0;j<n;j++) { cin >> mh[c] >> mr[c]; mc[c]=0; c++;}
	 }
 for (i=0;i<c;i++) for (j=0;j<c;j++) if (mc[j]==i) for (k=0;k<c;k++) if (mh[j]>mh[k] && mr[j]>mr[k]) mc[k]=i+1;
 for (i=0,m=0;i<c;i++) if (m<mc[i]) m=mc[i];
 cout << m+1 << endl;
 }
 return 0;
 }

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

struct DOLL{
	int h;
	int r;
};

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r)
{
	int ans = 0;
	for (DOLL &d : d1){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, d.h, d.r)+1;
			ans = max(ans, a);
		}
	}
	for (DOLL &d : d2){
		if (d.h>h && d.r>r){
			int a = f(d1, d2, d.h, d.r)+1;
			ans = max(ans, a);
		}
	}
	return ans;
}

int main() {

	while (true){
		vector<DOLL> d1;
		vector<DOLL> d2;
		int n,m;

		cin >> n;
		if (n == 0) break;

		for (int i = 0; i<n; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d1.push_back(d);
		}

		cin >> m;

		for (int i = 0; i<m; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d2.push_back(d);
		}

		int ans = f(d1, d2, 0, 0);
		cout << ans << endl;
	}

	return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;

int main(){
  int n;
  while(scanf("%d", &n) && n){
    vector<pair<int, int> >d;
    pair<int, int>t;
    int h, r;
    rep(i, n){
      scanf("%d %d", &h, &r);
      t = make_pair(h, r);
      d.push_back(t);
    }
    scanf("%d", &n);
    rep(i, n){
      scanf("%d%d", &h, &r);
      t = make_pair(h, r);
      d.push_back(t);
    }
    sort(d.begin(), d.end());

    rep(i, d.size()-1) if(d[i].first >= d[i+1].first || d[i].second >= d[i+1].second) d.erase(d.begin()+i+1);

    printf("%d\n", (int)d.size());
  }
  return 0;
}