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

typedef pair<int,int> P;

int main(){
	int n,m,h,r;
	vector<P> mat;
	vector<int> lh,lr;
	while(cin >> n,n){
		for(int i = 0;i < n;i++) {
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		cin >> m;
		for(int i = n;i < n + m;i++){
			cin >> h >> r;
			mat.push_back(P(h,r));
		}
		sort(mat.begin(),mat.end());
		for(int i = 0;i < mat.size();i++){
			if(lh.empty()) {
				lh.push_back(mat[i].first);
				lr.push_back(mat[i].second);
			}
			else{
				if(lr.back() < mat[i].second) {
					if(lh.back() < mat[i].first) {
						lh.push_back(mat[i].first);
						lr.push_back(mat[i].second);
					}
				}
				else {
					int pos = lower_bound(lr.begin(),lr.end(),mat[i].second) - lr.begin();
					lh[pos] = mat[i].first;
					lr[pos] = mat[i].second;
				}
			}
		}
		cout << lh.size() << endl;
		lh.clear();
		lr.clear();
		mat.clear();
	}
	return 0;
}

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

vector<pair<int,int> >v;
int dp[202];
int dfs(int i){
    if(dp[i]!=-1)return dp[i];
    int ret=0;
    for(int j=0;j<v.size();j++){
        if(v[j].first>v[i].first&&v[j].second>v[i].second){
            ret=max(ret,dfs(j)+1);
        }
    }
    return dp[i]=ret;
}
int main(){
    int n;
    while(cin>>n,n){
        v.clear();
        int m,x,y,x1,y1,x2,y2,a=1,c=1;
        for(int i=0;i<202;i++)dp[i]=-1;
        for(int i=0;i<n;i++){
            cin>>x>>y;
            v.push_back(make_pair(x, y));
        }
        cin>>m;
        for(int i=0;i<m;i++){
            cin>>x>>y;
            v.push_back(make_pair(x, y));
        }
        int ans=0;
        for(int i=0;i<m+n;i++){
            ans=max(ans,dfs(i)+1);
        }
        cout<<ans<<endl;
    }
}

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[201], dp2[201];
  P doll[200];
  while(cin >> n && n){
    fill(doll, doll+200, P(0, 0));
    fill(dp, dp + 201, INF);
    fill(dp2, dp2 + 201, 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++){
      int *p;
      p = lower_bound(dp, dp + 201, doll[i].S);
	if(doll[i].F != dp2[p-1-dp]){
	  *p = doll[i].S;
	  dp2[p-dp]=doll[i].F;
	}
    }
    cout << lower_bound(dp, dp + 201, INF) - dp << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<cstdlib>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define MAX 206
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],dp[j+1]+1);
	 }
      }
   }

   cout << dp[n+m] << 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<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;
typedef pair<int,int> P;
int n,m;
int dp[201];
int ans=0;
vector<P> v;
int main(void){
	while(1){
		ans=0;
		v.clear();
		cin >> n;
		if(n==0)break;
		for(int i=0;i<n;i++){
			int a,b;
			cin >> a >> b;
			v.push_back(P(a,b));
		}
		cin >> m;
		for(int i=0;i<m;i++){
			int a,b;
			cin >> a >> b;
			v.push_back(P(a,b));
		}
		sort(v.begin(),v.end());
		for(int i=0;i<v.size();i++)dp[i]=1;
		for(int i=0;i<v.size();i++){
			for(int j=0;j<v.size();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<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] && i>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*/