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 <bits/stdc++.h>
using namespace std;

struct doll{
    int h,r;
};

bool operator<(const doll& d1,const doll& d2){
    if(d1.h == d2.h){
        return d1.r < d2.r;
    }
    return d1.h < d2.h;
}

int main() {

    while(1){

        int n;

        cin >> n;

        if(n==0) break;

        int h,r;

        vector<doll> v;
        doll tmp;

        for(int i=0;i<n;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }

        int m;

        cin >> m;

        for(int i=0;i<m;i++){
            cin >> h >> r;
            tmp.h = h;
            tmp.r = r;
            v.push_back(tmp);
        }

        /*for(int i=0;i<n+m;i++){
            cout << v[i].h << " " << v[i].r << endl;
        }*/

        sort(v.begin(),v.end());

        int table[201][201];

        for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                table[i][j] = 0;
            }
        }

        /*for(int i=0;i<n+m;i++){
            table[i][i] = 1;
        }*/

        for(int i=0;i<n+m;i++){
            for(int j=i;j<n+m;j++){
                if( (v[i].h<v[j].h && v[i].r < v[j].r) /*|| (v[i].h > v[j].h && v[i].r > v[j].r)*/ ){
                    table[i][j] = 1;
                    //table[j][i] = 1;
                }
            }
        }

        /*for(int i=0;i<n+m;i++){
            for(int j=0;j<n+m;j++){
                cout << table[i][j] << " ";
            }
            cout << endl;
        }*/

        int dp[201][201] = {0};

        for(int i=0;i<=n+m;i++){
            dp[i][0] = 1;
            dp[0][i] = 1;
        }

        for(int i=1;i<=n+m;i++){
            for(int j=1;j<=n+m;j++){
                if(table[i-1][j-1]==1){
                    dp[i][j] = max(dp[i-1][j],dp[i-1][j-1]+1);
                }
                else{
                    dp[i][j] = dp[i-1][j];
                }
            }

        }

        /*for(int i=0;i<=n+m;i++){
            for(int j=0;j<=n+m;j++){
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }*/

        int ans = 0;

        for(int i=0;i<=n+m;i++){
            //cout << dp[i] << endl;
            ans = max(dp[i][n+m],ans);
        }

        cout << ans << endl;


    }

    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999

int main(){
    int n,m;
    bool data[1000][1000]={};
    
    while(cin>>n&&n){
        rep(i,1000)rep(j,1000)data[i][j]=false;
        rep(i,n){
            int a,b;
            cin>>a>>b;
            data[a][b]=true;
        }
        cin>>m;
        rep(i,m){
            int a,b;
            cin>>a>>b;
            data[a][b]=true;
        }
        
        
        int dp[1002][1002];
        rep(i,1002)rep(j,1002)dp[i][j]=0;
        
        
        int maxi = 0;
        for(int i=999;i>=0;i--){
            for(int j=999;j>=0;j--){
                dp[i][j] = max({dp[i][j+1],dp[i+1][j],dp[i+1][j+1]+data[i][j],dp[i][j]});
                //            cout<<dp[i][j]<<endl;
                maxi = max(maxi,dp[i][j]);
            }
        }
        cout<<maxi<<endl;
    }
}

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 <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct doll{
    int first, second;
    doll(int first, int second) : first(first), second(second) { }
};
bool operator < (const doll &a, const doll &b){
    return a.first*2+a.second==b.first*2+b.second ? (a.first==b.first ? a.second < b.second : a.first < b.first) : a.first*2+a.second < b.first*2+b.second;
}

int main(){
    while(true){
        int n, m;
        cin >> n;
        if(n==0) break;
        vector<doll> v;
        for(int i=0; i<n; i++){
            int h, r;
            cin >> h>> r;
            v.push_back(doll(h, r));
        }
        cin >> m;
        for(int i=n; i<n+m; i++){
            int h, r;
            cin >> h>>r;
            v.push_back(doll(h, r));
        }
        sort(v.begin(), v.end());
        ll dp[1010];
        fill(dp, dp+1001, 1);
        
        ll ans=0;
        
        for(int i=0; i<n+m; i++){
            for(int j=0; j<i; j++){
                if(v[i].first-v[j].first>0 && v[i].second-v[j].second>0){
                    dp[i]=max(dp[j]+1, dp[i]);
                }
            }
            //cout << "dp[" << v[i].first << ", " << v[i].second << "]=" << dp[i] << "\n";
            ans=max(ans, dp[i]);
        }
        
        cout << ans << "\n";
        
        
        
    }
    
    
    return 0;
}