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 <algorithm>
using namespace std;

class Doll{
public:
    int h;
    int r;
    int have;
    // static bool Less(const Doll& l, const Doll& r){
    // 	return l.h < r.h;
    // }
    bool operator < (const Doll &d) const{
	if( r == d.r ) return h < d.h;
	else return r < d.r;
    }
    Doll(int x, int y, int z){
	h = x; r = y; have = z;
    }
};

typedef vector<Doll> Dolls;

int main(){
    int n, m, h, r, result=0;
    Dolls dolls;
    while(cin >> n, n){
	for(int i=0; i<n; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	cin >> m;
	for(int i=0; i<m; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
//	sort(dolls.begin(), dolls.end(), Doll::Less);
	sort(dolls.begin(), dolls.end());
	for(Dolls::iterator it1=dolls.begin();it1 != dolls.end(); ++it1){
	    for(Dolls::iterator it2=dolls.begin();it2 != it1; ++it2){
		if(it2->h < it1->h && it2->r < it1->r && it1->have <= it2->have){
		    it1->have = it2->have + 1;
//		    result = max(result, it1->have);
		}
	    }
	}
	for(Dolls::iterator it=dolls.begin();it != dolls.end(); ++it)
	    result = max(result, it->have);
	cout << result << endl;
	dolls.clear();
	result = 0;
    }
    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];
????P doll[2000];
????while(cin >> n && n){
????????fill(doll, doll+2000, P(0, 0));
????????fill(dp, dp + 2000, 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);
????????for (int i = 0; i < n + m; i++){
????????*lower_bound(dp, dp + 2000, doll[i].S) = doll[i].S;
????????}
??????????int ans;
????????????????for(int i=n+m-1; i>=0; --i){
????????????????????????if(dp[i] != INF){
????????????????????????????????ans = i + 1;
????????????????????????????????break;
????????????????????????}
????????????????}
????????cout << ans << endl;
????}
????return 0;
}

C++: #include<iostream>

using namespace std;

int main(void){
  int n,m,h[101],r[101];

  while(cin >> n,n){
    for(int i=0;i<n;i++)cin >> h[i] >> r[i];
    cin >>m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(h[i]>h[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }
    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(r[i]>r[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }

    int dp[101]={0};
    dp[0]=1;
    for(int i=0;i<n+m;i++){
      for(int j=i+1;j<n+m;j++){
	if(h[i]<h[j] && r[i]<r[j] && dp[i]>=dp[j])dp[j]=dp[i]+1;
      }
    }
    int res=0;
    for(int i=0;i<n+m;i++)res=max(res,dp[i]);
    cout << res << endl;
  }

  return 0;
}

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 solve(int 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);}
    }

    return ans;
}
int main()
{
    int n,m;
    while(cin>>n)
    {
        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>

using namespace std;

int main(void){
  int n,m,h[201],r[201];

  while(cin >> n,n){
    for(int i=0;i<n;i++)cin >> h[i] >> r[i];
    cin >>m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

      for(int i=0;i<n+m;i++){
	  for(int j=i;j<n+m;j++){
	    int t;
	      if(h[i]>h[j]){
		  t=h[i];
		  h[i]=h[j];
		  h[j]=t;
		  t=r[i];
		  r [i]=r[j];
		  r[j]=t;
		}
	      if(h[i]==h[j] && r[i]>r[j]){
		  t=h[i];
		  h[i]=h[j];
		  h[j]=t;
		  t=r[i];
		  r [i]=r[j];
		  r[j]=t;
		}
	    }
	}

    int dp[201]={0};
    dp[0]=1;
    for(int i=0;i<n+m;i++){
      for(int j=i+1;j<n+m;j++){
	if(h[i]<h[j] && r[i]<r[j] && dp[i]>=dp[j])dp[j]=dp[i]+1;
      }
    }
    int res=0;
    for(int i=0;i<n+m;i++)res=max(res,dp[i]);
    cout << res << endl;
  }

  return 0;
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 30;
int n, m, p;
pair<int, int> doll[201];
int dp[2][201], dp2[201];
int main(){
	while (cin >> n){
		if (!n)
			return 0;
		fill(dp[0], dp[0] + 201, INF);
		fill(dp[1], dp[1] + 201, INF);
		fill(dp2, dp2 + 201, INF);
		for (int i = 0; i < n; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			int a, b;
			cin >> a >> b;
			doll[i].first = a;
			doll[i].second = b;
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			p = lower_bound(dp[0], dp[0] + n + m, doll[i].first) - dp[0];
			if (dp[1][p] > doll[i].second){
				dp[0][p] = doll[i].first;
				dp[1][p] = doll[i].second;
			}
		}
		for (int i = 0; i < n + m; i++){
			if (dp[1][i] == INF) break;
			*lower_bound(dp2, dp2 + n + m, dp[1][i]) = dp[1][i];
		}
		cout << lower_bound(dp2, dp2 + n + m, INF) - dp2 << endl;
	}
}