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 <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
//#include <tuple>
#include <set>
#include <string.h>
#include <functional>

#define X first
#define Y second
#define MP make_pair
//#define MT make_tuple
#define REP(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
#define FOR(i, n) REP(i, 0, n)
typedef long long ll;
using namespace std;
const int MAX = 101;

template<class T, class U>
void convert(T &t, U &u)
{
	stringstream ss;
	ss << t;
	ss >> u;
}

int dp[1001][1001];

int main()
{
	int n;
	while ( cin >> n && n ){
		memset(dp, 0, sizeof(dp));
		//入力.
		vector<pair<int, int> > data;
		FOR( i, n ){
			int s, t; cin >> s >> t;
			data.push_back( MP( s, t ) );
		}

		int m; cin >> m;
		FOR( i, m ){
			int s, t; cin >> s >> t;
			data.push_back( MP( s, t ) );
		}
		
		sort(data.begin(), data.end());


		dp[data[0].first][data[0].second] = 1;
		FOR(i, data.size()){
			REP(j, i + 1, data.size()){
				if (data[i].first < data[j].first && data[i].second < data[j].second){
					dp[data[j].first][data[j].second] = dp[data[i].first][data[i].second] + 1;
				}
			}
		}

		int maxc = -1;
		FOR(i, data.size()){
			maxc = max(maxc, dp[data[i].first][data[i].second]);
		}

		cout << maxc << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<queue>
using namespace std;
#define MAX_N 1000
int x[MAX_N], y[MAX_N], X[MAX_N][MAX_N], dist[MAX_N];
int A, B, N;
int main() {
	while (true) {
		cin >> A; if (A == 0) { break; } for (int i = 0; i < A; i++) { cin >> x[i] >> y[i]; }
		cin >> B; for (int i = B; i < A + B; i++) { cin >> x[i] >> y[i]; }N = A + B;
		for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (x[i] < x[j] && y[i] < y[j]) { X[i][j] = 1; } } }
		queue<int>Q; int res = 0; for (int i = 0; i < N; i++) { dist[i] = 0; Q.push(i); }
		while (true) {
			int a1 = Q.front(); Q.pop();
			for (int i = 0; i < N; i++) {
				if (X[a1][i] == 1 && dist[a1] + 1>dist[i]) { dist[i] = dist[a1] + 1; Q.push(i); }
			}
		}
		for (int i = 0; i < N; i++) { if (res < dist[i]) { res = dist[i]; } }cout << res << endl;
	}
}

C++: #include<iostream>

using namespace std;

const int MAX = 201;

typedef struct{
  int h;
  int r;
}DOLL;

int dp[MAX];
DOLL data[MAX];
int m,n;

void Dsort(){
  for(int i = 0; i < m+n-1; i++){
    for(int j = i; j < m+n; j++){
      if(data[i].r > data[j].r /*&& data[i].h > data[j].h*/){
	DOLL tmp  = data[i];
	data[i] = data[j];
	data[j] = tmp;
      }
    }
  }
}

int solve(){
  dp[1] = 1;
  DOLL now = data[0];
  for(int i = 2; i < m+n; i++){
    if(data[i].h > now.h && data[i].r > now.r){
      dp[i] = dp[i-1]+1;
      now = data[i];
    }else dp[i] = dp[i-1];
  }
  return dp[m+n-1]+1;
}


int main(){

  while(cin >> n && n){
    fill(dp,dp+MAX,0);
    for(int i = 0; i < n; i++) cin >> data[i].h >> data[i].r;
    cin >> m;
    for(int i = n; i < m+n; i++) cin >> data[i].h >> data[i].r;
    Dsort();
    cout << solve() << endl;
  }
  return 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] = 0;
            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> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,m;
  while(true){
      cin>>n;
      if(n==0) break;
      std::vector<P> v;
      for(int i=0;i<n;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      cin>>m;
      for(int i=0;i<m;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      sort(v.begin(),v.end());
      int res=0,hmin=-1,rmin=-1;
      for(int i=0;i<(int)v.size();i++){
          int h=v[i].first,r=v[i].second;
          if(hmin<h&&rmin<r){
              rmin=r;hmin=h;
              res++;
          }
      }
      cout<<res<<endl;
  }
}