Problem:
Score : 400 points

Problem Statement

On a two-dimensional plane, there are N red points and N blue points.
The coordinates of the i-th red point are (a_i, b_i), and the coordinates of the i-th blue point are (c_i, d_i).

A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.

At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.

Constraints

All input values are integers.

1 \leq N \leq 100

0 \leq a_i, b_i, c_i, d_i < 2N

a_1, a_2, ..., a_N, c_1, c_2, ..., c_N are all different.

b_1, b_2, ..., b_N, d_1, d_2, ..., d_N are all different.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
a_2 b_2
:
a_N b_N
c_1 d_1
c_2 d_2
:
c_N d_N

Output

Print the maximum number of friendly pairs.

Sample Input 1

3
2 0
3 1
1 3
4 2
0 4
5 5

Sample Output 1

2

For example, you can pair (2, 0) and (4, 2), then (3, 1) and (5, 5).

Sample Input 2

3
0 0
1 1
5 2
2 3
3 4
4 5

Sample Output 2

2

For example, you can pair (0, 0) and (2, 3), then (1, 1) and (3, 4).

Sample Input 3

2
2 2
3 3
0 0
1 1

Sample Output 3

0

It is possible that no pair can be formed.

Sample Input 4

5
0 0
7 3
2 2
4 8
1 6
8 5
6 9
5 4
9 1
3 7

Sample Output 4

5

Sample Input 5

5
0 0
1 1
5 5
6 6
7 7
2 2
3 3
4 4
8 8
9 9

Sample Output 5

4

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define LINE_BUF_SIZE 32

int main(){

    int N;
    int i,j,k;
    char line[LINE_BUF_SIZE];

    fgets(line,sizeof(line),stdin);
    sscanf(line,"%d", &N);

    int *r_x = (int *)malloc(sizeof(int) * (N+1));  /*動的確保してみる*/
    int *r_y = (int *)malloc(sizeof(int) * (N+1)); 
    int *b_x = (int *)malloc(sizeof(int) * (N+1)); 
    int *b_y = (int *)malloc(sizeof(int) * (N+1));

    //int r_x[N+1],r_y[N+1],b_x[N+1],b_y[N+1];

    int x,y;

    for(i=1;i<=N;i++){
        fgets(line,sizeof(line),stdin);
        sscanf(line,"%d %d", &x,&y);
        r_x[i] = x;
        r_y[i] = y;
    }

    for(i=1;i<=N;i++){
        fgets(line,sizeof(line),stdin);
        sscanf(line,"%d %d", &x,&y);
        b_x[i] = x;
        b_y[i] = y;
    }

    int b_x_max = 0,max_index = 0;
    int *sorted_b_x = (int *)malloc((sizeof(int)*N));
    int *sorted_b_y = (int *)malloc((sizeof(int)*N));

    for(i=1;i<=N;i++){          /*x座標が小さい青から探索しなければならないのでソート*/
        for(j=1;j<=N;j++){
            if(b_x_max <= b_x[j]){
                max_index = j;
                b_x_max = b_x[max_index];
            }
        }
        sorted_b_x[N-i+1] = b_x[max_index];
        sorted_b_y[N-i+1] = b_y[max_index];
        b_x[max_index] = -1;
        b_y[max_index] = -1;
        max_index = 0;
        b_x_max = 0;
    }

    int max_y = 0;
    int max = 0;
    int flag = 0;
    int count = 0;

    for(i=1;i<=N;i++){
        for(j=1;j<=N;j++){
            
            if(r_x[j] <= sorted_b_x[i] && r_y[j] <= sorted_b_y[i]){   /*まずペアになれるかどうか*/
                if(r_y[j] >= max){       /*ペアの中での最大のy座標を探索*/

                max = r_y[j];
                max_y = j;
                flag = 1;

                }
            }
        }

        if(flag == 1){
            r_y[max_y] = 1000;     /*pairになった赤点を除外*/
            r_x[max_y] = 1000;
            count += 1;
            flag = 0;
        }

        max  = 0;

    }

    printf("%d",count);

    free(r_x);
    free(r_y);
    free(b_x);
    free(b_y);
    free(sorted_b_x);
    free(sorted_b_y);

    return 0;

}