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++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 31;
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;
	}
}

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]++;
    }

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]++;
    }

    rep(i,1003) rep(j,1003){
      if(i>0 && j>0){
	dp[i][j]=max(dp[i-1][j],dp[i][j-1])+doll[i][j];
	if(doll[i][j]){
	  for(int k=j+1;k<1003;k++)
	    dp[i][k]=dp[i][j];
	  break;
	}
      }
    }

    cout<<dp[1000][1000]<<endl;
  }
}

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>
#include<algorithm>
using namespace std;
int n,m,tmp,ans;
int x[200],y[200],dp[200];
int check(int a,int b){
  return (x[a]>x[b]&&y[a]>y[b]);
}
int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++)cin>>x[i]>>y[i];
    cin>>m;
    for(int i=0;i<m;i++)cin>>x[n+i]>>y[n+i];
    for(int i=n+m-1;i>0;i--){
      for(int j=0;j<i;j++){
	if(x[j]*y[j]>x[j+1]*y[j+1]){
	  swap(x[j],x[j+1]);//tmp=x[j];x[j]=x[j+1];x[j+1]=tmp;
	  swap(y[j],y[j+1]);//tmp=y[j];y[j]=y[j+1];y[j+1]=tmp;
	}
      }
    }
    ans=1;
    for(int i=0;i<n+m;i++)dp[i]=1;
    for(int i=1;i<n+m;i++){
      for(int j=0;j<i;j++){
	if(check(i,j))dp[i]=max(dp[i],dp[j]+1);
      }
      ans=max(ans,dp[i]);
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int map[1010][1010];
int dp[1010][1010];
int main()
{
    while(1){
        int n;
        cin>>n;if(!n)return 0;
        for(int i=0;i<1010;++i){
            for(int j=0;j<1010;++j){
                map[i][j]=0;
                dp[i][j]=0;
            }
        }
        for(int i=0;i<n;++i){
            int h,r;
            cin>>h>>r;
            map[h+1][r+1]=1;
        }
        int m;
        cin>>m;
        for(int i=0;i<m;++i){
            int h,r;
            cin>>h>>r;
            map[h+1][r+1]=1;
        }
        for(int i=1;i<1002;++i){
            for(int j=1;j<1002;++j){
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                if(map[i][j]){
                    dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
                }
            }
        }
        cout<<dp[1001][1001]<<endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>P;
int main(){
	for(int n;cin>>n,n;){
		vector<P>V;
		for(int a,b;n--;){
			cin>>a>>b;
			V.push_back(P(a,b));
		}
		cin>>n;
		for(int a,b;n--;){
			cin>>a>>b;
			V.push_back(P(a,b));
		}
		V.push_back(P(0,0));
		sort(V.begin(),V.end());
		int dp[201]={0};
		int ans=0;
		
		for(int i=0;i<V.size();i++){
			ans = max(ans,dp[i]);
			for(int j=i+1;j<V.size();j++){
				if(V[i].first<V[j].first && V[i].second<V[j].second){
					dp[j] = max(dp[i]+1,dp[j]);
				}
			}
		}
		cout<<ans<<endl;
	}
	
	
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main(){
  int i,a,b,n,m,hn[100],rn[100],hm[100],rm[100],h[100],r[100],k[2];
  while(1){
    cin >> n;
    if(n == 0) break;
    for(i=0;i<n;i++) cin >> hn[i] >> rn[i];
    cin >> m;
    for(i=0;i<m;i++) cin >> hm[i] >> rm[i];

    a = 0;
    b = 0;
    for(i=0;i<m+n;i++){
      if(a < n && b < m && hn[a] < hm[b] || a < n && b >= m){
	  h[i] = hn[a];
	  r[i] = rn[a++];
	}
      else{
	h[i] = hm[b];
	r[i] = rm[b++];
      }
    }

      k[0] = 1;
      for(i=1;i<m+n;i++){
	if(h[i] > h[i-1] && r[i] > r[i-1]) k[0]++;
      }

    for(i=0;i<m+n;i++){
      if(a < n && b < m && rn[a] < rm[b] || a < n && b >= m){
	  h[i] = hn[a];
	  r[i] = rn[a++];
	}
      else{
	h[i] = hm[b];
	r[i] = rm[b++];
      }
    }

      k[1] = 1;
      for(i=1;i<m+n;i++){
	if(h[i] > h[i-1] && r[i] > r[i-1]) k[1]++;
      }

      if(k[0] > k[1]) cout << k[0] << endl;
      else cout << k[1] << endl;

  }
  return 0;
}