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>
#include<algorithm>
#include<climits>
#include<string>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstring>
#include<stack>
#include<functional>
using namespace std;

struct Doll{
	int h,r;
	bool operator <(const Doll &a)const{
		return h<a.h;
	}
};

int main(){
	int N,M;
	while(cin>>N,N){
		vector<Doll> d;
		for(int i=0;i<N;i++){
			Doll in; cin>>in.h>>in.r; d.push_back(in);
		}
		cin>>M;
		for(int i=0;i<M;i++){
			Doll in; cin>>in.h>>in.r; d.push_back(in);
		}

		sort(d.begin(),d.end());
		int dp[201][1000] = {}, ans = 0;
		for(int i=0,cnt=0; cnt<d.size(); cnt++){
			if(cnt!=0 && d[cnt-1].h!=d[cnt].h) i++;
			for(int j=0;j<d[cnt].r;j++){
				ans = max(ans, dp[i+1][d[cnt].r] = max(dp[i+1][d[cnt].r], dp[i][j]+1));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
P doll[200];
int memo[200];
int n,m;
int solve(int x) {
	if(memo[x]>-1) return memo[x];
	int sum=0;
	for(int i=x+1;i<(n+m);i++) {
		if(doll[x].second<doll[i].second)
			sum=max(sum,1+solve(i));
	}
	return memo[x]=sum;
}
int main() {
	while(scanf("%d",&n),n) {
		for(int i=0;i<200;i++) {
			doll[i].first=doll[i].second=1<<30;
		}
		for(int i=0;i<n;i++) {
			int h,r;
			scanf("%d %d",&h,&r);
			doll[i].first=h,doll[i].second=r;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++) {
			int h,r;
			scanf("%d %d",&h,&r);
			doll[i+n].first=h,doll[i+n].second=r;
		}
		sort(doll,doll+n+m);
		memset(memo,-1,sizeof(memo));
		int sum=0;
		for(int i=0;i<(n+m);i++) {
			sum=max(sum,1+solve(i));
		}
		printf("%d\n",sum);
	}
}

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

using namespace std;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(true) {
        vector<pair<int,int> > v;
        for(int i = 0; i < 2; ++i) {
            int N;
            cin >> N;
            if(!N) goto end;
            while(N--) {
                int H, R;
                cin >> H >> R;
                v.push_back(make_pair(H, R));
            }
        }
        sort(v.begin(), v.end());
        const int N = v.size();
        int ans = 1;
        vector<int> dp(N, 1);
        for(int i = 0; i < N; ++i) {
            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;
    }
end:
    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++){ // P©çÍ¶ßÄ¢éÉÓ
      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]Í·³ð²×ézñ 
  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 <cstring>
#include <vector>
#include <algorithm>
using namespace std;

typedef pair<int, int> doll;

bool cmpr(doll &a, doll &b)
{
 	if( a.first == b.first )
		return a.second < b.second;
	else
		return a.first < b.first;
}

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);
		}
		
		sort(vc.begin(), vc.end(), cmpr);
		
		dp[0] = 1;
		int surface = 0;

		for( i = 1; i < n+m; i++)
		{
			
			if( vc[surface].first < vc[i].first && vc[surface].second < vc[i].second )
			{
				dp[i] = dp[surface] + 1;
				surface = i;
			}
			else
			{
				dp[i] = dp[surface];
			}
			
//			cout << "dp::" << dp[i] << endl;
		}
		
		
		cout << dp[n+m-1] << endl;
	}
	
	return 0;
}

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

int main(){
  int i,j,n,m,ans;
  int h[200],r[200],dp[200];
  for(;;){
    cin >> n;
    if(!n)break;

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

    for(i=0;i<n+m;i++)
      for(j=i+1;j<n+m;j++)
	if(h[i]>h[j] || (h[i]==h[j] && r[i]>r[j])){
	  swap(h[i],h[j]);
	  swap(r[i],r[j]);
	}
   
    ans = 0;
    dp[0] = 1;
    for(i=1;i<n+m;i++){
      dp[i] = 1;
      for(j=0;j<i;j++)
	if(h[i]>h[j] && r[i]>r[j])dp[i] = max(dp[i],dp[j]+1);
      ans = max(ans,dp[i]);
    }
    cout << ans << endl;
  }
}