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>

using P = std::pair<int, int>;

int n;

P dolls[200];

int dp[200];

int main() {
	while (1) {
		int m;
		std::cin >> n;
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		std::cin >> m;
		for (int i = n; i < n + m; ++i)
			std::cin >> dolls[i].first >> dolls[i].second;
		n += m;
		std::sort(dolls, dolls + n);
		dp[0] = 1;
		for (int i = 1; i < n; ++i) {
			dp[i] = dp[i - 1];
			for (int j = i - 1; j >= 0; --j) {
				if (dolls[i].first > dolls[j].first&&
					dolls[i].second > dolls[j].second) {
					dp[i] = std::max(dp[j] + 1, dp[i]);
				}
			}
		}
		std::cout << dp[n - 1] << std::endl;
	}
	return 0;
}

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[2000], dp2[2000];
  P doll[2000];
  while(cin >> n && n){
    fill(doll, doll+2000, P(0, 0));
    fill(dp, dp + 2000, INF);
    fill(dp2, dp2 + 2000, 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 + 2000, 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 + 2000, INF) - dp << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

struct data{
  ll h,r,s;
  bool operator<(const data& d){
    return s < d.s;
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<data> v;
    ll h,r;
    rep(i,n){
      cin >> h >> r;
      v.push_back({h,r,h+r});
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      v.push_back({h,r,h+r});
    }
    sort(all(v));
    ll ans = 0,c = 0;
    vvl(dp,1002,1002,0);
    REP(i,1,1001){
      REP(j,1,1001){
        if(c >= n+m) break;
        if(v[c].h <= i && v[c].r <= j){
          dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
          ++c;
        }
        else{
          dp[i][j] = max(dp[i][j],max(dp[i][j-1],max(dp[i-1][j-1],dp[i-1][j])));
        }
      }
    }
    rep(i,30){
      rep(j,30){
//         cout << dp[i][j] << ' ';
        ans = max(ans,dp[i][j]);
      }
//       cout << endl;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

class mycmp{
public:
	bool operator()(const P p1, const P p2){
		if(p1.first < p2.first) return true;
		else if(p1.second < p2.second) return true;
		else false;
	}
};

int main(){
	int n;

	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}


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

		int* dp = new int[n+m];
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n+m;i++) dp[i] = 1;

		for(int i=1;i<dolls.size();i++){
			for(int j=i-1;j>=0;j--){
				if(dolls[j].first < dolls[i].first
					&& dolls[j].second < dolls[i].second){
						dp[i] = max(dp[i],dp[j] + 1);
				}
				else dp[i] = dp[i-1];
			}
		}

		cout << dp[dolls.size()-1] << endl;
		delete[] dp;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int n, m;
int dp[200];
pair<int, int> data[200];

int main() {
	while (cin >> n, n) {
		REP(i, n) scanf("%d %d", &data[i].first, &data[i].second);
		cin >> m;
		FOR(i, n, n + m) scanf("%d %d", &data[i].first, &data[i].second);
		
		sort(data, data + n + m);
		fill(dp, dp + n + m, 1);
		
		REP(i, n + m) REP(j, i) {
			if (data[i].first > data[j].first && data[i].second > data[j].second) {
				dp[i] = max(dp[i], dp[j] + 1);
			}
		}
		
		int ans = -1;
		REP(i, n + m) ans = max(ans, dp[i]);
		cout << ans << endl; 
	}
	return 0;
}