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<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int, int> pii;
#define MAX 99999999
int main(){
    vvi d(1040,vi(1040,0));
    vvi dp(1040,vi(1040,0));
    int m,n; cin>>m;
    rep(i,n){
        pii temp; cin>>temp.first>>temp.second;
        d[temp.first][temp.second]++;
    }
    cin>>m;
    rep(i,m){
        pii temp;
        cin>>temp.first>>temp.second;
        d[temp.first][temp.second]++;
    }
    loop(i, 1, 1001)
    loop(j,1,1001){
        dp[i][j]=max(max(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]+d[i][j]);
                     
    }
    
}

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

typedef pair<int,int> P;

bool involve(P a, P b) {
  return (a.first<b.first && a.second<b.second);
}

int main() {
  int n, m, s, t;
  int a[200];
  P dolls[201];
  while (1) {
    cin >> n;
    if (!n) break;
    dolls[0].first = dolls[0].second = 0;
    for (int i=1; i<=n; i++) {
      cin >> dolls[i].first >> dolls[i].second;
    }
    cin >> m;
    for (int i=1; i<=m; i++) {
      cin >> dolls[i+n].first >> dolls[i+n].second;
    }
    s = n+m;
    sort(dolls, dolls+s);
    for (int i=0; i<=s; i++) {
      a[i] = 0;
    }
    for (int i=1; i<=s; i++) {
      for (int j=i-1; j>=0; j--) {
        if (involve(dolls[j], dolls[i])) {
          a[i] = max(a[i], a[j]+1);
        }
      }
    }
    t = 0;
    for (int i=1; i<=s; i++) {
      if (t < a[i]) {
        t = a[i];
      }
    }
    cout << t << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repb(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int dp[1010][1010];
bool doll[1010][1010];
int n, m, h, r;

signed main(){
	while (cin >> n && n) {
		rep(i, 0, 1010) {
			rep(j, 0, 1010) {
				doll[i][j] = false;
				dp[i][j] = 0;
			}
		}
		rep(i, 0, n) {
			cin >> h >> r;
			doll[h][r] = true;
		}
		cin >> m;
		rep(i, 0, m) {
			cin >> h >> r;
			doll[h][r] = true;
		}
		rep(i, 1, 1010) {
			rep(j, 1, 1010) {
				if (doll[i][j]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
				else {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}
		cout << dp[1001][1001] << endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<cstdlib>
using namespace std;
struct doll{
    int r;
    int h;
};
vector<doll> dolls;
int hen[1010][1010];
int dollnum;
int dp[210];

int solve(int n)
{
    if(dp[n]!= -1){ return dp[n];}
    int ans = 0;
    int temp=0;
    for(int i=0;i<dollnum;i++)
    {
        if(hen[n][i]==1) {temp = solve(i)+1; ans = max(temp,ans);}
    }
    dp[n] = ans;
    return ans;
}
int main()
{
    int n,m;
    while(cin>>n)
    {
        memset(dp,-1,sizeof(dp));
        dolls.clear();
        memset(hen,0,sizeof(hen));
        if(n==0)break;
        doll temp;
        temp.h=0;temp.r=0;
        dolls.push_back(temp);
        for(int i=0;i<n;i++)
        {
            cin>>temp.h>>temp.r;
            dolls.push_back(temp);
        }
        cin>>m;
        for(int i=0;i<m;i++)
        {
            cin>>temp.h>>temp.r;
            dolls.push_back(temp);
        }
        dollnum = dolls.size();
        for(int i=0;i<dollnum-1;i++)
        {
            for(int j=0;j<dollnum;j++)
            {
                if(dolls[i].h<dolls[j].h && dolls[i].r<dolls[j].r){hen[i][j] =1;}
                if(dolls[i].h>dolls[j].h && dolls[i].r>dolls[j].r){hen[j][i] =1;}
            }
        }
        cout << solve(0)<< 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] && 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*/