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

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

int main(){
  int n;

  while(cin >> n ,n){
    vector<P> v;

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

    sort(v.begin(),v.end());
    int dp[MAX] = {0};
    int ans = 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);
	}
      }
      ans = max(ans,dp[i]);
    }

    cout << ans << endl;
  }

  return 0;
}

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

typedef std::pair<int, int> P;

P data[202];
int dp[202];

int main(){

	int n, m;
	while (std::cin >> n, n){

		data[0] = P(0, 0);
		dp[0] = 0;
		for (int i = 1; i <= n; i++){
			std::cin >> data[i].first >> data[i].second;
		}

		std::cin >> m;
		for (int i = 1; i <= m; i++){
			std::cin >> data[i + n].first >> data[i + n].second;
		}

		std::sort(data, data + 1 + n + m);

		for (int i = 1; i <= n + m; i++){
			for (int j = 0; j < i; j++){
				if (data[i].first > data[j].first&&data[i].second > data[j].second){
					dp[i] = std::max(dp[i], dp[j] + 1);
				}
			}
		}

		int max = 0;
		for (int i = 0; i <= n + m; i++){
			if (dp[i] > max)max = dp[i];
		}
		std::cout << max << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <list>
#include <algorithm>
#define INF 10e6
#define F first
#define S second
using namespace std;

typedef pair<int,int> P;

int main(void){
	P dolls[200];
	P dolls1[200];
	int dp[200];
	int dp1[200];
	int n,m;
	while(cin >> n,n){
		for(int i=0;i<n;i++)
			cin >> dolls[i].F >> dolls[i].S;
		cin >> m;
		for(int i=n;i<n+m;i++)
			cin >> dolls[i].F >> dolls[i].S;
		for(int i=0;i<n+m;i++)
			dolls1[i].F=dolls[i].S,dolls1[i].S=dolls[i].F;
		sort(dolls,dolls+n+m);
		sort(dolls1,dolls1+n+m);
		int cx,cy;
		fill(dp,dp+n+m,INF);
		fill(dp1,dp1+n+m,INF);
		for(int i=0;i<n+m;i++){
			*lower_bound(dp,dp+n+m,dolls[i].S)=dolls[i].S;
			*lower_bound(dp1,dp1+n+m,dolls1[i].S)=dolls1[i].S;
		}
		cx=lower_bound(dp,dp+n+m,INF)-dp;
		cy=lower_bound(dp1,dp1+n+m,INF)-dp1;
		cout << max(cx,cy) << endl;
	}
	return 0;
}

C++: //============================================================================
// Name        : TopCoderCompetition.cpp
// Author      : taguchi
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <numeric>
using namespace std;
#define P pair<int,int>
#define rep(i,n) for(int i = 0;i<n;i++)
#define pb(n) push_back(n)
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
struct edge{int cost,to;};

vector<string> split(string &in,char delimiter){
	vector<string> re; uint cur = 0,next;
	while((next = in.find_first_of(delimiter,cur)) != string::npos){
		re.pb(string(in,cur,next-cur));
		cur = next + 1;
	}
	re.pb(string(in,cur,in.size()-cur));
	return re;
}
vector<int> split_int(string &in, char delimiter){
	vector<string> str = split(in,delimiter);
	vector<int> re;
	rep(i,str.size()){
		re.pb(strtol(str[i].c_str(),NULL,10));
	}
	return re;
}





int N,M;
int h[210],r[210];
int mem[210];
bool used[210];
int solve(int p){
	if(mem[p]) return mem[p];
	used[p] = true;
	int ans = 1;
	for(int i = 0;i<N+M;i++){
		if(h[p] > h[i] && r[p] > r[i] && !used[i]){
			ans = max(ans,solve(i) + 1);
		}
	}
	used[p] = false;
	return mem[p] = ans;
}

int main(){
	while(true){
		cin >> N;
		if(!N) break;
		rep(i,N){
			cin >> h[i] >> r[i];
		}
		cin >> M;
		rep(i,M){
			cin >> h[i+N] >> r[i+N];
		}
		memset(mem,0,sizeof(mem));
		int ans = 0;
		rep(i,N+M){
			ans = max(ans,solve(i));
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

int main(){/*main*/
  int i,n1,n2,j,res;

  for(i=0;i<N;i++){
    h1[i]=0;
    r1[i]=0;
  }

  while(1){

    cin >> n1; // êQÌ}g[VJÌ
    
    if(n1 == 0)break;
    
    for(i=0;i<n1;i++){
      cin >> h1[i] >> r1[i]; //(n1-1)ÜÅªêQ
    }
    
    cin >> n2; //ñYÌ}g[VJÌ
    
    for(i=n1;i<(n1+n2);i++){
      cin >> h1[i] >> r1[i]; //n1©ç(n1+n2-1)ÜÅªñY
    }
    
    sort(n1,n2); // ¬³¢ÉÀÑÖ¦
    
    //  for(i=0;i<n1+n2;i++){
    // cout << i << ":"<< h1[i] << " " << r1[i] <<endl;
    // }
    
    res=syori(n1,n2);
    
    
    
    cout << res << endl;
    res=0;
    
  }
  
}


void sort(int n1,int n2){
  int k,l,max,h;

  for(k=0;k<(n1+n2);k++){
    max=k;
    for(l=k;l<(n1+n2);l++){
      if(h1[l]<h1[max]){ max=l; }
    }
    h=h1[max];
    h1[max]=h1[k];
    h1[k]=h;
    
    h=r1[max];
    r1[max]=r1[k];
    r1[k]=h;
    
  }
  
  
}


int syori(int n1,int n2){
  int q,i,H[N],Hcount=0,ans=0,I[N],HH[N],count=1,kcount=0,korecount=0;
  
  
  for(q=0;q<N-5;q++){  
    H[q]=0;
    I[q]=0;
    HH[q]=0;
  }
  
  //cout << "test" << endl;
  
  for(q=0;q<(n1+n2);q++){
    
    cout <<"q--"<< q << " " <<"count--" << count << endl; 
    cout << "korecount--" << kcount <<endl;    
    
    if(h1[q]<h1[q+1] && r1[q]<r1[q+1]){
      count++; 
      korecount=0;
      
    }    
    if((h1[q]==h1[q+1] || r1[q] == r1[q+1]) && HH[q+1]==0 ){
      
  korecount=1; //2ñÈãÍ¢éº      

      if(korecount==1){} 
      else{ count++; }
      
    
 
 H[q+1]=count-1;
 HH[q+1]=1;
 Hcount++;
 I[q+1]=q;
 q+=1;
    }
    
    //  cout << Hcount << endl;
    
    
    if(q==(n1+n2-1) && Hcount!=0){
      Hcount=0;
      kcount=1;
      
      if(count>=ans)ans=count;
      
      // cout << "ans--" << ans << endl; 
      
      for(i=0;i<(n1+n2);i++){
	
	if(HH[i]==1){
	  count=H[i];
	  H[i]=0;
	  HH[i]=0;
	  q=I[i];
	  break;
	}
	
      }
      
      
    }
    
    
    
  }
 
  if(kcount==0){ ans=count; } // kcountªOÌÆ«Í¾ÔèÈµ
  else{ kcount=0; }           // ±ÌvOÅÍ¾ÔçÈ¢ÆansªÅÈ¢kokoayasii
  
  
  
  for(i=0;i<n1+n2;i++){
    cout << i << ":"<< h1[i] << " " << r1[i] <<endl;
  }
  
  
  return ans;
}

//ðÔáÁÄ¢Ü¹ñ©H FE( while(i=5)  CR[ª«èÈ¢)