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>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int, int>P;

P v1[200], v2[200];
bool used1[200], used2[200];
int main() {
	int n;
	while (scanf("%d", &n), n) {
		memset(used1, 0, sizeof(used1));
		memset(used2, 0, sizeof(used2));
		rep(i, n) {
			int h, r; scanf("%d%d", &h, &r);
			v1[i] = P(h, r);
		}
		int m; scanf("%d", &m);
		rep(i, m) {
			int h, r; scanf("%d%d", &h, &r);
			v2[i] = P(h, r);
		}
		sort(v1, v1 + n);
		sort(v2, v2 + m);
		v1[n] = v2[m] = P(2000, 2000);
		int cnt1 = 0;
		rep(i, n + 1) {
			rep(j, m) {
				if ((!i || (v1[i - 1].first < v2[j].first&&v1[i - 1].second < v2[j].second)) &&
					v2[j].first < v1[i].first&&v2[j].second < v1[i].second && !used1[j]) {
					used1[j] = true;
					cnt1++;
				}
			}
		}
		int cnt2 = 0;
		rep(i, m + 1) {
			rep(j, n) {
				if ((!i || (v2[i - 1].first < v1[j].first&&v2[i - 1].second < v2[j].second)) &&
					v1[j].first < v2[i].first&&v1[j].second < v2[i].second && !used2[j]) {
					used2[j] = true;
					cnt2++;
				}
			}
		}
		printf("%d\n", max(cnt1 + n, cnt2 + m));
	}
}

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

C++: #include<bits/stdc++.h>
#define mk make_pair
#define f first
#define s second
using namespace std;
int n,m,p1,p2;
map<pair<int,pair<int,int> >,int >m1;

vector<pair<int,int> >v;

int dfs(int d,int h,int w){
  int res=0;
  if(m1[mk(d,mk(h,w))])return m1[mk(d,mk(h,w))];
  if(d==v.size()) return 0;
  if(v[d].f<h&&v[d].s<w)
    res=max(res,dfs(d+1,v[d].f,v[d].s))+1;
  res=max(res,dfs(d+1,h,w));
  return m1[mk(d,mk(h,w))]=res;
}

main(){
  while(cin>>n,n){
    m1.clear();
    v.clear();
    for(int i=0;i<n;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    for(int i=0;i<v.size();i++)swap(v[i].f,v[i].s);
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    cout<<dfs(0,1001,1001)<<endl;
  }
}

C++: #include <iostream>
#include <sstream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef vector <int> VI;
typedef pair <int, int> PII;
typedef vector <PII> VPII;

double get_dist( PII a ) {
    double x = a.first * a.first;
    double y = a.second * a.second;
    return sqrt( x + y );
}

bool check( PII a, PII b, PII c ) {
    double da = get_dist( a );
    double db = get_dist( b );
    double dc = get_dist( c );
    bool f1 = a.first < b.first && b.first < c.first;
    bool f2 = a.second < b.second && b.second < c.second;
    bool f3 = da < db && db < dc;
    return f1 && f2 && f3;
}

int solve( VPII A, VPII B ) {
    int n = A.size();
    int m = B.size();
    int answer = n;

    if ( A[n-1].first < B[0].first && A[n-1].second < B[0].second ) {
        return n + m;
    }

    bool used[101];
    for ( int i = 0; i < m; ++ i ) {
        used[i] = false;
    }

    for ( int i = 0; i + 1 < n; ++ i ) {
        double mind = INT_MAX;
        int mini = -1;
        for ( int j = 0; j < m; ++ j ) {
            if ( used[j] ) continue;
            if ( ! check( A[i], B[j], A[i+1] ) ) continue;
            double d = get_dist( B[j] );
            if ( d < mind ) {
                mind = d;
                mini = j;
            }
        }
        if ( mini != -1 ) {
            used[mini] = true;
            answer ++;
        }
    }

    return answer;
}

int main() {
    int n, m;
    while ( cin >> n && n ) {
        VPII A( n );
        for ( int i = 0; i < n; ++ i ) {
            cin >> A[i].first >> A[i].second;
        }
        cin >> m;
        VPII B( m );
        for ( int i = 0; i < m; ++ i ) {
            cin >> B[i].first >> B[i].second;
        }
        cout << max( solve( A, B ), solve( B, A ) ) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  int n,m;
  int i,j;
  int h,r;

  while(1){

    int doll[1005][1005]={};
    int dp[1005][1005]={};

    scanf("%d",&n);
    if(n==0)break;
    rep(i,n){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }
  
    for(i=1;i<1001;i++){
      for(j=1;j<1001;j++){
	dp[i][j]=max(max(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]+doll[i][j]);
      }
    }
 
    cout<<dp[1000][1000]<<endl;
  }
}