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;
#define f first
#define s second
#define pb(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)
int main(){
    int n;
    while(cin>>n,n){
        vector<pair<int,int> >v;
        int m,x,y,a=1,c=1,b=101;
        g(0,n){
            cin>>x>>y;
            v.pb(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.pb(x, y);
        }
        sort(v.begin(),v.end());
        x=v[0].f,y=v[0].s;
        g(1,m+n){
            if(v[i].s>y&&v[i].f>x){
                a++;
                x=v[i].f,y=v[i].s;
            }
            else
                b=min(b,i);
        }
        if(b<101){
            x=v[b].f,y=v[b].s;
            g(b+1,m+n){
                if(v[i].s>y&&v[i].f>x){
                    c++;
                    x=v[i].f,y=v[i].s;
                }
            }
        }
        cout<<max(a,c)<<endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

vector< pair<int,int> > doll;
int dp[200];
int n,m,tmp1,tmp2;

int main(){
  while(cin>>n && n){
    for(int i=0;i<n;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>tmp1>>tmp2;
      doll.push_back(make_pair(tmp1,tmp2));
    }

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

    int ans=0;

    for(int i=0;i<n+m;i++){
      dp[i]=1;
      for(int j=i+1;j<n+m;j++){
        if(doll[i].second>doll[j].second){
          dp[i]=max(dp[i],dp[j]+1);
        }
      }
      ans=max(ans,dp[i]);
    }

    cout<<ans<<endl;

    while(doll.size()) doll.pop_back();
    memset(dp,0,sizeof(dp));
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int main(){
  int n,m;
  while(cin>>n&&n){
    int dp[210],cop[210];
    vector<int> v[1010];
    for(int i=0;i<n;i++){
      int x,y;cin>>x>>y;
      v[x].push_back(y);
      dp[i]=mod;
    }
    cin>>m;
    for(int i=0;i<m;i++){
      int x,y;cin>>x>>y;
      v[x].push_back(y);
      dp[i+n]=mod;
    }
    dp[n+m]=mod;
    for(int i=0;i<1010;i++){
      for(int j=0;j<=n+m;j++)cop[j]=dp[j];
      for(int j=0;j<v[i].size();j++){
	int it=lower_bound(dp,dp+n+m,v[i][j])-dp;
	cop[it]=min(cop[it],v[i][j]);
      }
      for(int j=0;j<=n+m;j++)dp[j]=cop[j];
    }
    int ans=0;
    while(true){
      if(dp[ans]==mod)break;
      ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<vector>
#include<utility>
#define rep(i,a) for( int i = 0; i != (a); ++i )

const int INF = 1 << 30;

typedef std::pair<int, int> P;

int n, m;
int dp[200];

int main()
{
	while( scanf( "%d", &n ), n )
	{
		memset( dp, 0, sizeof( dp ) );

		std::vector<P> ps;
		rep( i, n )
		{
			int h, r;
			scanf( "%d%d", &h, &r );
			ps.push_back( P( h, r ) );
		}

		scanf( "%d", &m );
		rep( i, m )
		{
			int h, r;
			scanf( "%d%d", &h, &r );
			ps.push_back( P( h, r ) );
		}

		std::sort( ps.begin(), ps.end() );

		int ans = 0;
		rep( i, ps.size() )
		{
			dp[i] = 1;
			
			rep( j, i )
				if( ps[i].first > ps[j].first && ps[i].second > ps[j].second )
					dp[i] = std::max( dp[i], dp[j]+1 );

			ans = std::max( ans, dp[i] );
		}

		printf( "%d\n", ans );
	}

	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <utility>

int N, M;
std::pair<int, int> Mat[201];
int dp[201];

int main(){
    while(scanf("%d", &N), N){
        memset(dp, -1, sizeof(dp));
        
        for(int i = 0; i < N; ++i)
            scanf("%d %d", &Mat[i].first, &Mat[i].second);
    
        scanf("%d", &M);
        
        for(int i = 0; i < M; ++i)
            scanf("%d %d", &Mat[i + N].first, &Mat[i + N].second);
        
        std::sort(Mat, Mat + (M + N));
        
        for(int i = 0; i < N + M; ++i)
            dp[i] = 1;
        
        int res = 1;
        for(int i = 0; i < N + M; ++i){
            for(int j = 0; j < i; ++j){
                if(Mat[i].first > Mat[j].first && Mat[i].second > Mat[j].second){
                    dp[i] = std::max(dp[i], dp[j] + 1);
                    
                    res = std::max(res, dp[i]);
                }
            }
        }
        
        std::cout << res << std::endl;
        
    }
    
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main(){
  int n,m,x,y;
  while(cin >>n,n){
    vector<pair<int, int> > doll;
    vector<int> lot;
    int dp[201] = {0};
    for(int i=0; i<n; i++){cin >>x>>y;doll.push_back(make_pair(x,y));}
    cin >>m;
    for(int i=0; i<m; i++){cin >>x>>y;doll.push_back(make_pair(x,y));}
    sort(doll.begin(),doll.end());
    for(int i=0,r=-1; i<doll.size(); i++){
      if(r != doll[i].first){
	r = doll[i].first;
	lot.push_back(doll[i].second);
      }
    }
    for(int i=0; i<lot.size(); i++){
      for(int j=1; j<=i+1; j++){
	if(dp[j-1]<lot[i]){
	  if(dp[j] == 0 || lot[i]<dp[j]){
	    dp[j] = lot[i];
	    break;
	  }
	}
      }
    }
    for(int ans=1;;ans++){
      if(dp[ans] == 0){cout <<ans-1<<endl;break;}
    }
  }
  return 0;
}