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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace ABC091C
{
    class Program
    {
        public static void Solve(Input input)
        {
            var n = input.NextInt();
            var xr = new int[n];
            var yr = new int[n];
            var xb = new int[n];
            var yb = new int[n];

            for (int i = 0; i < n; i++)
            {
                xr[i] = input.NextInt();
                yr[i] = input.NextInt();
            }
            for (int i = 0; i < n; i++)
            {
                xb[i] = input.NextInt();
                yb[i] = input.NextInt();
            }

            Array.Sort(xr, yr);
            Array.Sort(xb, yb);

            var d = new Dinic(n * 2 + 2);
            for (int i = 0; i < n; i++)
            {
                for (int j = Search.UpperBound(xb, xr[i]); j < n; j++)
                {
                    if (yr[i] < yb[j])
                    {
                        d.Add(i, n + j, 1);
                    }
                }
            }

            for (int i = 0; i < n; i++)
            {
                d.Add(n * 2, i, 1);
                d.Add(n + i, n * 2 + 1, 1);
            }

            Console.WriteLine(d.Flow(n * 2, n * 2 + 1));
        }

        /// <summary>
        /// 昇順ソート済みリストに対しての二分探索系
        /// </summary>
        static class Search
        {
            /// <summary>
            /// Bound系の下請け
            /// </summary>
            static int FindBound<T>(IList<T> list, T v, int begin, int end, Func<T, T, bool> judge) where T : IComparable<T>
            {
                int low = begin, high = end;

                if (low > high) { throw new ArgumentException(); }

                while (low < high)
                {
                    var mid = (high + low) / 2;

                    if (judge(v, list[mid])) { high = mid; }
                    else { low = mid + 1; }
                }

                return low;
            }

            /// <summary>
            /// v以上で最小のインデックスを返す
            /// </summary>
            public static int LowerBound<T>(IList<T> list, T v, int begin, int end) where T : IComparable<T>
                => FindBound(list, v, begin, end, (x, y) => x.CompareTo(y) <= 0);

            /// <summary>
            /// v以上で最小のインデックスを返す
            /// </summary>
            public static int LowerBound<T>(IList<T> list, T v) where T : IComparable<T>
                => LowerBound<T>(list, v, 0, list.Count());

            /// <summary>
            /// vより大きな最小のインデックスを返す
            /// </summary>
            public static int UpperBound<T>(IList<T> list, T v, int begin, int end) where T : IComparable<T>
                => FindBound(list, v, begin, end, (x, y) => x.CompareTo(y) < 0);

            /// <summary>
            /// vより大きな最小のインデックスを返す
            /// </summary>
            public static int UpperBound<T>(IList<T> list, T v) where T : IComparable<T>
                => UpperBound(list, v, 0, list.Count());

            /// <summary>
            /// 範囲中でvと等価な個数を返す
            /// </summary>
            public static int EqualRange<T>(IList<T> list, T v, int begin, int end) where T : IComparable<T>
                => UpperBound(list, v, begin, end) - LowerBound(list, v, begin, end);

            /// <summary>
            /// 範囲中でvと等価な個数を返す
            /// </summary>
            public static int EqualRange<T>(IList<T> list, T v) where T : IComparable<T>
                => UpperBound(list, v) - LowerBound(list, v);

            /// <summary>
            /// v以下で最大のインデックスを返す
            /// O(logN+N)なので注意
            /// </summary>
            public static int OrLess<T>(IList<T> list, T v, int begin, int end) where T : IComparable<T>
            {
                var i = LowerBound(list, v, begin, end);

                if (list[i].CompareTo(v) != 0)
                {
                    // v未満で最大の値とする
                    if (i > 0) i--;
                }
                else
                {
                    // vと同じ値で、一番最後へ移動させる
                    while (i < end && list[i + 1].CompareTo(v) == 0) { i++; }
                }

                return i;
            }

            /// <summary>
            /// v以下で最大のインデックスを返す
            /// O(logN+N)なので注意
            /// </summary>
            public static int OrLess<T>(IList<T> list, T v) where T : IComparable<T>
                => OrLess(list, v, 0, list.Count());

            /// <summary>
            /// v未満で最大のインデックスを返す
            /// </summary>
            public static int LessThan<T>(IList<T> list, T v, int begin, int end) where T : IComparable<T>
            {
                var i = LowerBound(list, v, begin, end);

                // v未満で最大の値とする
                return (i > 0) ? i - 1 : i;
            }

            /// <summary>
            /// v以下で最大のインデックスを返す
            /// </summary>
            public static int LessThan<T>(IList<T> list, T v) where T : IComparable<T>
                => LessThan(list, v, 0, list.Count());
        }

        /// <summary>
        /// 最大フローを求める
        /// 応用：2部最大マッチング
        /// </summary>
        class Dinic
        {
            static readonly int INF = int.MaxValue / 3;

            class Edge
            {
                public int To;
                // 逆辺
                public int Capacity;
                public int Rev;

                public Edge(int to, int cap, int r)
                {
                    this.To = to;
                    this.Capacity = cap;
                    this.Rev = r;
                }
            }

            readonly List<Edge>[] G;

            // sからの距離
            readonly int[] Level;

            // 調査済みフラグ
            readonly int[] Iter;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="n">ノード数</param>
            public Dinic(int n)
            {
                G = Enumerable.Range(0, n)
                    .Select(_ => new List<Edge>())
                    .ToArray();

                Level = new int[n];
                Iter = new int[n];
            }

            /// <summary>
            /// 流路追加
            /// </summary>
            /// <param name="from"></param>
            /// <param name="to"></param>
            /// <param name="cap"></param>
            public void Add(int from, int to, int cap)
            {
                G[from].Add(new Edge(to, cap, G[to].Count));
                G[to].Add(new Edge(from, 0, G[from].Count - 1));
            }

            void Bfs(int s)
            {
                for (int i = 0; i < Level.Length; i++)
                {
                    Level[i] = -1;
                }
                var q = new Queue<int>();

                Level[s] = 0;
                q.Enqueue(s);

                while (q.Any())
                {
                    var v = q.Dequeue();
                    for (int i = 0; i < G[v].Count(); i++)
                    {
                        var e = G[v][i];
                        if (e.Capacity > 0 && Level[e.To] < 0)
                        {
                            Level[e.To] = Level[v] + 1;
                            q.Enqueue(e.To);
                        }
                    }
                }
            }

            int Dfs(int v, int t, int f)
            {
                if (v == t) return f;
                for (int i = Iter[v]; i < G[v].Count(); Iter[v] = ++i)
                {
                    var e = G[v][i];
                    if (e.Capacity > 0 && Level[v] < Level[e.To])
                    {
                        int d = Dfs(e.To, t, Math.Min(f, e.Capacity));
                        if (d > 0)
                        {
                            e.Capacity -= d;
                            G[e.To][e.Rev].Capacity += d;
                            return d;
                        }
                    }
                }

                return 0;
            }

            /// <summary>
            /// 最大流量を調べる
            /// </summary>
            /// <param name="s">始点</param>
            /// <param name="t">終点</param>
            /// <returns></returns>
            public int Flow(int s, int t)
            {
                if (s == t)
                {
                    return 0;
                }

                int flow = 0;

                while (true)
                {
                    Bfs(s);
                    if (Level[t] < 0) return flow;
                    for (int i = 0; i < Iter.Length; i++)
                    {
                        Iter[i] = 0;
                    }

                    int f;
                    while ((f = Dfs(s, t, INF)) > 0)
                    {
                        flow += f;
                    }
                }
            }

            /// <summary>
            /// 始点と終点の探索
            /// 一意に定まると仮定して返答する
            /// </summary>
            /// <returns></returns>
            public Tuple<int, int> Find()
            {
                // 始点: 誰からも流れてきていない
                var s = Enumerable.Range(0, G.Count())
                    .Except(G.SelectMany(x => x.Select(y => y.To)))
                    .FirstOrDefault();

                // 終点: 誰へも流さない
                var t = Enumerable.Range(0, G.Count())
                    .FirstOrDefault(x => !G[x].Any(y => y.Capacity > 0));

                return new Tuple<int, int>(s, t);
            }
        }

        #region Main
        public static void Main(string[] args)
        {
            // 出力が少ないときはこれをセットする方が時間かかるけれど
            // そのときにTLEするのはアルゴリズムが悪いせいだし、まあ良しとする
            var needsFlushOutput = true;
            if (needsFlushOutput)
            {
                // 細かく出力しないようにする
                var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
                Console.SetOut(sw);
            }

            // 通常は引数無しで、ファイルから読み出したい場合はpath指定する
            var input = new Input();
            // 仮想的に標準入力をセットする
            input.SetText("");

            Solve(input);

            Console.Out.Flush();
        }
        #endregion

        #region Competitive Template
#pragma warning disable CS0414
        static readonly int MOD = (int)1e9 + 7;
        static readonly int[] dx = { 1, 0, -1, 0 };
        static readonly int[] dy = { 0, 1, 0, -1 };
        /// <summary> 左上が原点 </summary>
        static readonly char[] dir = { 'R', 'U', 'L', 'D' };
#pragma warning restore CS0414

        public class Input
        {
            // 変な改行コードがたまに混じっているので、一応セパレート指定する
            // スペース単独指定の方がもちろん早い
            static readonly char[] separator = { ' ', '\r', '\n' };
            readonly StreamReader sr;
            readonly Queue<string> queue;

            /// <summary>
            /// 特定のファイルから読み出したい場合はpath指定する
            /// </summary>
            public Input(string path = "")
            {
                queue = new Queue<string>();

                if (string.IsNullOrEmpty(path)) { sr = new StreamReader(Console.OpenStandardInput()); }
                else { sr = new StreamReader(path); }
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public void SetText(IEnumerable<string> items)
            {
                foreach (var item in items)
                    SetText(item);
            }

            /// <summary>
            /// 入力予約
            /// </summary>
            public bool SetText(string s)
            {
                if (string.IsNullOrEmpty(s)) return false;

                foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries))
                    queue.Enqueue(elem);

                return true;
            }

            /// <summary>
            /// 要素が存在するか
            /// </summary>
            public bool Any() => queue.Any() || Read();

            /// <summary>
            /// 内部queueに入力からの値をsplitして格納する
            /// </summary>
            bool Read()
            {
                if (!SetText(sr.ReadLine())) return false;

                if (!queue.Any()) return Read();

                return queue.Any();
            }

            /// <summary>
            /// 次のstringを一つ読み込む
            /// </summary>
            public string Next()
            {
                if (!queue.Any() && !Read()) return "";

                return queue.Dequeue();
            }

            /// <summary>
            /// 指定個数queueにたまるまでenqueueし続ける
            /// </summary>
            bool Accumulate(int n)
            {
                while (queue.Count() < n)
                    if (!Read()) return false;

                return true;
            }

            public int NextInt() => int.Parse(Next());
            public long NextLong() => long.Parse(Next());
            public double NextDouble() => double.Parse(Next());

            /// <summary>
            /// n個の要素をparseして、それぞれにoffsetをaddした配列を返す
            /// </summary>
            T[] NextT<T>(int n, T offset, Func<string, T> parse, Func<T, T, T> add)
            {
                if (!Accumulate(n)) return null;

                var a = new T[n];

                for (int i = 0; i < n; i++)
                    a[i] = add(parse(queue.Dequeue()), offset);

                return a;
            }

            public string[] Next(int n) => NextT(n, "", x => x, (x, y) => x);
            public int[] NextInt(int n, int offset = 0) => NextT(n, offset, int.Parse, (x, y) => x + y);
            public long[] NextLong(int n, long offset = 0) => NextT(n, offset, long.Parse, (x, y) => x + y);
            public double[] NextDouble(int n, double offset = 0.0) => NextT(n, offset, double.Parse, (x, y) => x + y);
        }

        static class Utils
        {
            public static T Max<T>(params T[] objs) => objs.Max();
            public static T Min<T>(params T[] objs) => objs.Min();

            /// <summary>
            /// vでfillされたT[d1][d2]配列を作成する
            /// </summary>
            public static T[][] Create2DArray<T>(int d1, int d2, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(v, d2).ToArray()).ToArray();
            }

            /// <summary>
            /// vでfillされたT[d1][d2][d3]配列を作成する
            /// </summary>
            public static T[][][] Create3DArray<T>(int d1, int d2, int d3, T v)
            {
                return
                    Enumerable.Repeat(0, d1).Select(_ =>
                    Enumerable.Repeat(0, d2).Select(__ =>
                    Enumerable.Repeat(v, d3).ToArray()).ToArray()).ToArray();
            }
        }
        #endregion
    }
}