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

int main(){
    int n, m;
    int d[200][2], dp[200];
    
    for(int p = 0; true; p++){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        swap(d[j][0], d[j - 1][0]);
                        swap(d[j][1], d[j - 1][1]);
                    }
                }
            }
            int maax = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]){
                        if(d[j][1] < d[i][1]){
                            a = dp[j];
                        }
                    }
                }
                dp[i] += a;
                if(maax <= dp[i]){
                    maax = dp[i];
                }
            }
            cout << maax << endl;
        }else{
            break;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef pair<int, int> P;
const int INF = 1e+9;

int DP[1000];

int main() {
	int n, m, h, r;
	while(cin >> n && n){
		memset(DP, 0, sizeof(DP));
		vector<P> vec;
		for(int i = 0; i < n; ++i){
			cin >> h >> r;
			vec.push_back(P(h, r));
		}
		cin >> m;
		for(int i = 0; i < m; ++i){
			cin >> h >> r;
			vec.push_back(P(h, r));
		}
		sort(vec.begin(), vec.end());
		int ans = 0;
		for(int i = 0; i < (n + m); ++i){
			DP[i] = 1;
			for(int j = 0; j < i; ++j){
				if(vec[j].first < vec[i].first && vec[j].second < vec[i].second)
					DP[i] = max(DP[i], DP[j] + 1);
			}
			ans = max(ans, DP[i]);
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<cstdio>

using namespace std;

int main(){/*main*/
  int i,j,k,n,h[201],hn2,hen,r[201],m,hn,count,ans[201],min,max;

  while(1)
    {/*while*/
      max=0;
      hen=0;
      count=0;
      h[0]=-1;
      r[0]=-1;
      for(i=0;i<201;i++)
    {
      ans[i]=0;
    }
      ans[0]=0;

      cin >> n ;
      if(n==0)break;
      for(i=1;i<=n;i++)
    {
      cin >> h[i] >> r[i];
    }

      cin >> m;
      for(i=n+1;i<=(m+n);i++)
    {
      cin >> h[i] >> r[i];
    }

      /*
      for(i=1;i<(n+m);i++)
      {
      printf("TES_h%d %d  TES_r%d %d\n",i,h[i],i,r[i]);
      }
      */
     


      for(i=1;i<=(m+n);i++)
    {/*for1*/
      min=i;
      for(j=i;j<=(m+n);j++)
        {/*for2*/
          if(h[j]<h[min])
        {
          min=j;
        }
        }/*E_for2*/
      hn=h[min];
      h[min]=h[i];
      h[i]=hn;

      hn2=r[min];
      r[min]=r[i];
      r[i]=hn2;
    }/*E_for1*/

 
      //    for(i=1;i<=(n+m);i++)
      // {
      //	printf("TES_h[%d] %d  TES_r[%d] %d\n",i,h[i],i,r[i]);
      // }
     

       /*   for(i=1;i<=(m+n);i++)
    {
      if(h[i]>h[i-1] && r[i]>r[i-1])
        {
          count+=1;
          ans[i]=1;
        }
      else
        {
          ans[i-1]=0;
          for(j=1;j<=i;j++)
        {
          if((h[i]>h[j] && r[i]>r[j]) && ans[j]==0 &&(h[i]!=h[i-1] && r[i]!=r[i-1]))
            {
              hen++;
            }

        }
          if(hen>0)count++;
        }
*/


       for(i=1;i<=(m+n);i++)
	 {
	   k=0;
	   for(j=1;j<i;j++)
	     {
	       if(r[i]>r[j] && r[j]>r[k] && ans[j]>ans[k])
		 {
		   k=j;
		 }
	       // cout << "TES_max"<< max << endl;
	     }
	   ans[i]=ans[k]+1;
	 }
       
       max=-1;
       for(i=1; i<=n+m; i++)
	 {
	   if(max<ans[i])
	     {
	       max=ans[i];
	     }
	 }

   
       
       
       cout << max << endl;
       
       
    }/*E_while*/
  return 0;
}/*E_main*/

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define ALL(c) (c).begin(), (c).end() 
using namespace std;

struct doll {
	int h, r; // h, r <= 1,000
	doll() {}
	bool operator<(const doll& right) const {
		return h == right.h ? r < right.r : h < right.h;
	}
	bool bigger_than(const doll d) {
		return h > d.h && r > d.r;
	}
};

#define MAX 100

int n, m; // n, m <= 100
vector<doll> dolls;
int dp[MAX * 2]; // dp[i]: i??????????????¢?????????????????£????????????

int main()
{	
	int maxval, i;
/************** input **************/
	while(cin >> n, n) {
		for(i = 0; i < n; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
		cin >> m;
		for(i = 0; i < m; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
/************** solve **************/
		sort(ALL(dolls));
#if 0
		for(int i = 0; i < n + m; i++)
			cout << dolls.at(i).h << " " << dolls.at(i).r << endl;
#endif
		for(i = 0; i < n + m; i++) {
			maxval = 0;
			for(int j = i - 1; j >= 0; j--) 
				if(dolls.at(i).bigger_than(dolls.at(j)))
					maxval = max(maxval, dp[j]);
			dp[i] = maxval + 1;
		}
/************** output **************/
		maxval = 0;
		for(i = n + m - 1; i >= 0; i--)
			maxval = max(maxval, dp[i]);
		cout << maxval << endl;
	}
}

C++: #include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;

typedef pair<int, int> P;
const int INF = 1e+9;

int DP[1000];

int main() {
	int n, m, h, r;
	while(cin >> n && n){
		memset(DP, 0, sizeof(DP));
		vector<P> vec;
		for(int i = 0; i < n; ++i){
			cin >> h >> r;
			vec.push_back(P(h, r));
		}
		cin >> m;
		for(int i = 0; i < m; ++i){
			cin >> h >> r;
			vec.push_back(P(h, r));
		}
		sort(vec.begin(), vec.end());
		int ans = 0;
		for(int i = 0; i < (n + m); ++i){
			DP[i] = 1;
			for(int j = 0; j < i; ++j){
				if(vec[j].first < vec[i].first && vec[j].second < vec[i].second)
					DP[i] = max(DP[i], DP[j] + 1);
			}
			ans = max(ans, DP[i]);
		}
		cout << ans << endl;
	}
	
	return 0;
}