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.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Numerics;


#if DEBUG
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

namespace competitive_programming
{
    public class Program
    {
        static void Main(string[] args)
        {
#if DEBUG
            var scanner = new IO.StreamScanner(File.Open("input.txt", FileMode.Open));
#else
            var scanner = new IO.StreamScanner(Console.OpenStandardInput());
#endif

            var n = scanner.Integer();

            var reds = new List<Tuple<int, int, int>>();
            var blues = new List<Tuple<int, int, int>>();

            for(int i=0; i<n; i++)
            {
                reds.Add(new Tuple<int, int, int>(i, scanner.Integer(), scanner.Integer()));
            }
            for (int i = 0; i < n; i++)
            {
                blues.Add(new Tuple<int, int, int>(i + 101, scanner.Integer(), scanner.Integer()));
            }

            var matching = new BipartiteMatching();

            foreach(var red in reds)
            {
                foreach(var blue in blues)
                {
                    if (red.Item2 < blue.Item2 && red.Item3 < blue.Item3)
                        matching.AddPair(blue.Item1, red.Item1);
                }
            }

            IO.Printer.Out.WriteLine(matching.GetMaxMatchingNum());
            IO.Printer.Out.Flush();
        }
    }

    /// <summary>
    /// get max matching between left side nodes and right side nodes
    /// </summary>
    public class BipartiteMatching
    {
        Dinic fordFulkerson;
        HashSet<int> startVertice = new HashSet<int>();
        HashSet<int> targetVertice = new HashSet<int>();

        public BipartiteMatching()
        {
            fordFulkerson = new Dinic();
        }

        public void AddPair(int leftSide, int rightSide)
        {
            fordFulkerson.AddEdge(leftSide, rightSide, 1);

            startVertice.Add(leftSide);
            targetVertice.Add(rightSide);
        }

        public double GetMaxMatchingNum()
        {
            int start = fordFulkerson.MaxIdx + 1;
            int target = start + 1;

            foreach (var s in startVertice)
            {
                fordFulkerson.AddEdge(start, s, 1);
            }
            foreach (var t in targetVertice)
            {
                fordFulkerson.AddEdge(t, target, 1);
            }

            return fordFulkerson.GetMaxFlow(start, target);
        }
    }

    /// <summary>
    /// get max flow from s to t
    /// </summary>
    public class Dinic
    {
        public class Edge
        {
            public int V;
            public double C;
            public Edge N;
            public Edge B;
        }

        Edge[] _pool = new Edge[0];
        int _topIdx;

        Edge[] _adj = new Edge[0];

        public int MaxIdx;
        int _minV;

        public Dinic()
        {
            Expand(ref _pool, 128, true);
            Expand(ref _adj, 128, false);
            Init();
        }

        public void Init()
        {
            for (int i = 0; i < _adj.Length; i++) _adj[i] = null;
            _topIdx = 0;
            MaxIdx = 0;
            _minV = Int32.MaxValue;
        }

        void Expand(ref Edge[] edges, int size, bool fillWithEmptyEdge = false)
        {
            if (edges.Length <= size)
            {
                var newEdges = new Edge[Math.Max(size, edges.Length * 2)];
                for (int i = 0; i < edges.Length; i++) newEdges[i] = edges[i];
                if (fillWithEmptyEdge)
                {
                    for (int i = edges.Length; i < newEdges.Length; i++)
                    {
                        newEdges[i] = new Edge();
                    }
                }
                edges = newEdges;
            }
        }

        public void AddEdge(int u, int v, double c, double bc = 0)
        {
            MaxIdx = Math.Max(MaxIdx, Math.Max(u + 1, v + 1));
            _minV = Math.Min(_minV, Math.Min(u, v));

            Expand(ref _pool, _topIdx + 1, true);
            Expand(ref _adj, MaxIdx);

            _pool[_topIdx].V = v;
            _pool[_topIdx].C = c;
            _pool[_topIdx].N = _adj[u];
            _adj[u] = _pool[_topIdx];
            _topIdx++;

            _pool[_topIdx].V = u;
            _pool[_topIdx].C = bc;
            _pool[_topIdx].N = _adj[v];
            _adj[v] = _pool[_topIdx];
            _topIdx++;

            _adj[u].B = _adj[v];
            _adj[v].B = _adj[u];

            if (u == v)
            {
                _adj[u].N.B = _adj[u];
                _adj[v].B = _adj[v].N;
            }
        }

        int[] _d;
        int[] _q;
        int _qh;
        int _qt;
        Edge[] _cur;

        public double GetMaxFlow(int s, int t)
        {
            MaxIdx = Math.Max(MaxIdx, Math.Max(s + 1, t + 1));
            _minV = Math.Min(_minV, Math.Min(s, t));

            _d = new int[MaxIdx];
            _q = new int[MaxIdx];
            _cur = new Edge[MaxIdx];

            double f = 0;

            while (ReLabel(s, t))
            {
                for (int i = 0; i < MaxIdx; i++) _cur[i] = _adj[i];
                f += Augument(s, t, s, double.MaxValue);
            }

            return f;
        }

        bool ReLabel(int s, int t)
        {
            for (int i = 0; i < MaxIdx; i++) _d[i] = Int32.MaxValue;
            _d[_q[_qh = _qt = 0] = t] = 0;
            while (_qh <= _qt)
            {
                var u = _q[_qh++];

                for (var i = _adj[u]; i != null; i = i.N)
                {
                    if (i.B.C != 0 && _d[i.V] > _d[u] + 1)
                    {
                        _d[i.V] = _d[u] + 1;
                        if ((_q[++_qt] = i.V) == s)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        double Augument(int s, int t, int u, double e)
        {
            if (u == t) return e;
            double f = 0;

            for (var i = _cur[u]; i != null; i = i.N)
            {
                if (i.C > 0 && _d[u] == _d[i.V] + 1)
                {
                    var df = Augument(s, t, i.V, Math.Min(e, i.C));
                    if (df > 0)
                    {
                        i.C -= df;
                        i.B.C += df;
                        e -= df;
                        f += df;
                    }
                }
                if (!(e > 0)) break;
            }

            return f;
        }

        // (S, T)
        public Tuple<List<int>, List<int>> GetStCut(int s)
        {
            var used_flg = new bool[MaxIdx];

            Dfs(s, used_flg);

            var ret_s = new List<int>();
            var ret_t = new List<int>();
            for (int i = _minV; i < MaxIdx; i++)
            {
                if (!used_flg[i])
                {
                    ret_t.Add(i);
                }
                else
                {
                    ret_s.Add(i);
                }
            }

            return new Tuple<List<int>, List<int>>(ret_s, ret_t);
        }

        void Dfs(int u, bool[] used_flg)
        {
            if (used_flg[u]) return;
            used_flg[u] = true;

            for (var i = _adj[u]; i != null; i = i.N)
            {
                if (i.C > 1e-6) Dfs(i.V, used_flg);
            }
        }
    }
}

#if DEBUG
#endif

#region INOUT

namespace IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;

    public class Printer : StreamWriter
    {
        static Printer()
        {
            Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false };
        }

        public static Printer Out { get; set; }

        public override IFormatProvider FormatProvider
        {
            get { return CultureInfo.InvariantCulture; }
        }

        public Printer(Stream stream)
            : base(stream, new UTF8Encoding(false, true))
        {
        }

        public Printer(Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
        }

        public void Write<T>(string format, T[] source)
        {
            base.Write(format, source.OfType<object>().ToArray());
        }

        public void WriteLine<T>(string format, T[] source)
        {
            base.WriteLine(format, source.OfType<object>().ToArray());
        }
    }

    public class StreamScanner
    {
        public StreamScanner(Stream stream)
        {
            str = stream;
        }

        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof;

        public bool IsEndOfStream
        {
            get { return isEof; }
        }

        private byte read()
        {
            if (isEof) return 0;
            if (ptr < len) return buf[ptr++];
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++];
            isEof = true;
            return 0;
        }

        public char Char()
        {
            byte b;
            do b = read(); while ((b < 33 || 126 < b) && !isEof);
            return (char)b;
        }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }

        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }

        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0;
            byte b;
            var ng = false;
            do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-')
            {
                ng = true;
                b = read();
            }
            for (; ; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                ret = ret * 10 + b - '0';
            }
        }

        public int Integer()
        {
            return (isEof) ? int.MinValue : (int)Long();
        }

        public double Double()
        {
            var s = Scan();
            return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN;
        }

        static T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n)
        {
            return enumerate(n, Char);
        }

        public string[] Scan(int n)
        {
            return enumerate(n, Scan);
        }

        public double[] Double(int n)
        {
            return enumerate(n, Double);
        }

        public int[] Integer(int n)
        {
            return enumerate(n, Integer);
        }

        public long[] Long(int n)
        {
            return enumerate(n, Long);
        }
    }
}

    #endregion