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 (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict

INF = float("inf")

def getlist():
	return list(map(int, input().split()))


from collections import deque
class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)








    def bfs(self, s, t):
        self.level = level = [None]*self.N
        deq = deque([s])
        level[s] = 0
        G = self.G
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        return level[t] is not None

    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow


def main():
	N = int(input())
	A = []
	B = []
	for i in range(N):
		a, b = getlist()
		A.append([a, b])
	for i in range(N):
		c, d = getlist()
		B.append([c, d])

	G = Dinic(2 * N + 2)
	for i in range(N):
		G.add_edge(0, i + 1, 1)
		G.add_edge(N + 1 + i, 2 * N + 1, 1)
	for i in range(N):
		for j in range(N):
			if A[i][0] < B[j][0] and A[i][1] < B[j][1]:
				G.add_edge(i + 1, N + 1 + j, 1)
	print(G.flow(0, 2 * N + 1))


if __name__ == '__main__':
	main()

Solution (Python):
import sys
from operator import itemgetter
input = sys.stdin.readline

def main():
    N = int(input())
    red = [list(map(int, input().split())) for _ in range(N)]
    blue = [list(map(int, input().split())) for _ in range(N)]
    red = sorted(red, key=itemgetter(0), reverse=True)
    blue = sorted(blue, key=itemgetter(1))
    visited = [False] * N
    ans = 0
    for i in blue:
        c = i[0]
        d = i[1]
        for j in range(N):
            a = red[j][0]
            b = red[j][1]
            if a < c and b < d and not visited[j]:
                ans += 1
                visited[j] = True
                break
    print(ans)


if __name__ == '__main__': main()

Solution (Python):
import heapq

N = int(input())
R = [list(map(int,input().split())) for _ in range(N)]
B = [list(map(int,input().split())) for _ in range(N)]
R.sort()
B.sort()



ans = 0
RS = [False for _ in range(N)]
for bx,by in B:
  pair = []
  for i in range(N):
    rx,ry = R[i]
    if rx > bx:
      break
    if ry > by or RS[i]:
      continue
    heapq.heappush(pair,[-ry,i])
  if len(pair) > 0:
    _,idx = heapq.heappop(pair)
    RS[idx] = True
    ans += 1
print(ans)

Solution (Python):
N = int(input())

R = [list(map(int, input().split())) for _ in range(N)]
B = [list(map(int, input().split())) for _ in range(N)]

B.sort(key=lambda x: x[0])
R.sort(key=lambda x: x[0])

res = 0
for b in B:

    targets = []
    for i in range(len(R)):
        r = R[i]
        if r[0] < b[0] and r[1] < b[1]:
            targets.append(r)
        if r[0] >= b[0]:
            break

    if len(targets) == 0:
        continue
    targets.sort(key=lambda x: x[1], reverse=True)
    R.remove(targets[0])
    res += 1
print(res)

Solution (Python):
N = int(input())
rls = []
rlog = [0]*N
bls = []
blog = [0]*N
for i in range(N):
  a, b = map(int, input().split())
  rls += [(a,b)]
for i in range(N):
  a, b = map(int, input().split())
  bls += [(a,b)]

bls.sort(key=lambda x:x[0])
ans = 0
for i in range(N):
  c, d = bls[i]
  mb = -1
  mj = 0
  for j in range(N):
    a, b = rls[j]
    if a<c and b<d and rlog[j]==0:
      if mb<b:
        mb = b
        mj = j
  if mb!=-1:
    rlog[mj] = 1
    ans += 1
  
print(ans)

Solution (Python):
N = int(input())

R = [list(map(int, input().split())) for _ in range(N)]
B = [list(map(int, input().split())) for _ in range(N)]

R = sorted(R, key=lambda x: -x[1])  
B = sorted(B, key=lambda x: x[0])  

for a, b in B:  
    for c, d in R:  
        if a > c and b > d:  
            R.remove([c, d])
            break

print(N - len(R))

Solution (Python):
N = int(input())
ab = []
for i in range(N):
    _a, _b = map(int, input().split())
    ab.append((_a, _b))
cd = []
for j in range(N):
    _c, _d = map(int, input().split())
    cd.append((_c, _d))
cd.sort()

s = set()
for j in range(N):
    maxb = -1
    maxi = -1
    for i in range(N):
        if ab[i][0] < cd[j][0] and ab[i][1] < cd[j][1] and i not in s:
            if ab[i][1] > maxb:
                maxb = ab[i][1]
                maxi = i
    if maxi != -1:
        s.add(maxi)
print(len(s))

Solution (Python):
n=int(input())

red={}
for _ in range(n):
  x,y=map(int,input().split())
  if x in red:
    red[x].append(y)
  else:
    red[x]=[y]
  
blue={}
for _ in range(n):
  x,y=map(int,input().split())
  if x in blue:
    blue[x].append(y)
  else:
    blue[x]=[y]

blueOption=[2*n+1,[]]

cnt=0
for i in range(2*n+1):
  border=2*n-i
  blueOption[0]=border
  if border in blue:
    blueOption[1]+=blue[border]
    blueOption[1].sort()
  
  
  if border in red:
    redTemp=red[border]
    
    redTemp.sort(reverse=True)
    for item in redTemp:
      for blueItem in blueOption[1]:
        if item<blueItem:
          cnt+=1
          blueOption[1].remove(blueItem)
          break
print(cnt)

Solution (Python):
N = int(input())
ab = [[0, 0] for i in range(N)]
b = [0]*N
cd = [[0, 0] for i in range(N)]
d = [0]*N

for i in range(N):
    ab[i] = list(map(int, input().split()))
for i in range(N):
    cd[i] = list(map(int, input().split()))

points = [[] for i in range(N)]
points2 = [[] for i in range(N)]
cd.sort(key=lambda x: x[0])
ab.sort(key=lambda x: x[1], reverse=True)

points.sort(key=lambda x: len(x))
ans = 0

for i in range(N):
    for j in range(N):
        if ab[i][0] < cd[j][0] and ab[i][1] < cd[j][1]:
            ans += 1
            cd[j][0] = -1
            break
print(ans)

Solution (Python):
def main():
    N = int(input())
    red = []
    for _ in range(N):
        a, b = map(int, input().split())
        red.append([a, b, 0])

    blue = []
    for _ in range(N):
        a, b = map(int, input().split())
        blue.append([a, b, 1])

    rb = red + blue
    rb.sort(key = lambda x: x[0])
    

    used = set()
    count = 0
    for i in range(2*N):
        if rb[i][2] == 1:
            min_j = None
            max_y = -1
            for j in range(i):
                if rb[j][2] == 0 and j not in used:
                    if max_y < rb[j][1] < rb[i][1]:
                        max_y = rb[j][1]
                        min_j = j
            if min_j != None:
                
                used.add(min_j)
                count += 1
    return count

if __name__ == '__main__':
    print(main())

Solution (Python):
from operator import itemgetter

N = int(input())
AB = sorted([list(map(int,input().split())) for i in range(N)])
CD = sorted([list(map(int,input().split())) for i in range(N)])
inf = float('inf')
ans = 0

for bx, by in CD:
    S = list(filter(lambda n:n[0]<bx, AB))
    S = list(filter(lambda n:n[1]<by, S))
    if len(S) != 0:
        S.sort(key=itemgetter(1))
        AB[AB.index(S[-1])] = [inf, inf]
        ans += 1
print(ans)

Solution (Python):
from operator import itemgetter

N = int(input())
AB = sorted([list(map(int,input().split())) for i in range(N)])
CD = sorted([list(map(int,input().split())) for i in range(N)])
inf = float('inf')
ans = 0

for bx, by in CD:
    S = list(filter(lambda n:n[0]<bx, AB))
    S = list(filter(lambda n:n[1]<by, S))
    if len(S) != 0:
        S.sort(key=itemgetter(1))
        AB[AB.index(S[-1])] = [inf, inf]
        ans += 1
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    N = int(input())
    A = sorted(
        [tuple(map(int, input().split())) for _ in range(N)],
        key=lambda x: x[1],
        reverse=1,
    )
    B = sorted([tuple(map(int, input().split())) for _ in range(N)])
    ans, memo = 0, set()
    for bx, by in B:
        for i, (ax, ay) in enumerate(A):
            if i not in memo and ax < bx and ay < by:
                ans += 1
                memo |= {i}
                break
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
r = [list(map(int, input().split())) for _ in range(N)]
b = [list(map(int, input().split())) for _ in range(N)]

r.sort(key=lambda val:val[1], reverse=True)
b.sort()

ans = 0

for bb in b:
    for i in range(N):
        if bb[0] > r[i][0] and bb[1] > r[i][1]:
            ans += 1
            N -= 1
            r.pop(i)
            break

print(ans)

Solution (Python):

import sys
from collections import defaultdict, deque

inf = float('inf')


class Dinic(object):
    def __init__(self):
        self.V = set()
        self.adj = defaultdict(list)
        self.c = defaultdict(lambda: 0)
        self.f = defaultdict(lambda: 0)
        self.iter = defaultdict(lambda: 0)
    
    def add_edge(self, u, v, cap):
        self.V.add(u)
        self.V.add(v)
        self.adj[u].append(v)
        self.adj[v].append(u)
        self.c[(u, v)] = cap
    
    def bfs(self, s):
        self.level = {v: inf for v in self.V}
        self.level[s] = 0
        Q = deque([s])
        while len(Q) > 0:
            u = Q.popleft()
            for v in self.adj[u]:
                if self.c[(u, v)] <= 0:
                    continue
                if self.level[v] <= self.level[u]:
                    continue
                self.level[v] = self.level[u] + 1
                Q.append(v)
    
    def dfs(self, u, t, mincap):
        if u == t:
            return mincap

        for i in range(self.iter[u], len(self.adj[u])):
            self.iter[u] = i
            v = self.adj[u][i]
            if self.c[(u, v)] <= 0:
                continue
            if self.level[v] <= self.level[u]:
                continue
            d = self.dfs(v, t, min(mincap, self.c[(u, v)]))
            if d > 0:
                self.c[(u, v)] -= d
                self.c[(v, u)] += d
                return d
        return 0

    def max_flow(self, s, t):
        flow = 0
        
        while True:
            self.bfs(s)

            if self.level[t] == inf:
                return flow
        
            self.iter = defaultdict(lambda: 0)
            d = self.dfs(s, t, inf)
            while d > 0:
                flow += d
                d = self.dfs(s, t, inf)


def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]", d: "List[int]"):
    G = Dinic()
    for xr, yr in zip(a, b):
        G.add_edge(0, (xr, yr), 1)
        for xb, yb in zip(c, d):
            if xr < xb and yr < yb:
                G.add_edge((xr, yr), (xb, yb), 1)
    for xb, yb in zip(c, d):
        G.add_edge((xb, yb), 1, 1)
    print(G.max_flow(0, 1))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int()] * (N)  
    b = [int()] * (N)  
    for i in range(N):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    c = [int()] * (N)  
    d = [int()] * (N)  
    for i in range(N):
        c[i] = int(next(tokens))
        d[i] = int(next(tokens))
    solve(N, a, b, c, d)

if __name__ == '__main__':
    main()

Solution (Python):
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])

N = int(input())

R = []
for _ in range(N):
    x, y = map(int, input().split())
    R.append(Point(x, y))

B = []
for _ in range(N):
    x, y = map(int, input().split())
    B.append(Point(x, y))

E = [[] for _ in range(N)]
for i, r in enumerate(R):
    for j, b in enumerate(B):
        if r.x < b.x and r.y < b.y:
            E[i].append(j)


class BipartiteMatching:
    def __init__(self, max_u: int, edges):
        self.max_u = max_u
        self.edges = edges  
        self.__match = None

    def dfs(self, v: int, visited: set) -> bool:
        for u in self.edges[v]:
            if u in visited:
                continue
            visited.add(u)

            if self.__match[u] == -1 or self.dfs(self.__match[u], visited):
                self.__match[u] = v
                return True
        return False

    def execute(self):
        self.__match = [-1 for _ in range(self.max_u)]
        return sum(self.dfs(u, set()) for u in range(self.max_u))


print(BipartiteMatching(N, E).execute())

Solution (Python):
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])


N = int(input())

R = []
for _ in range(N):
    x, y = map(int, input().split())
    R.append(Point(x, y))

B = []
for _ in range(N):
    x, y = map(int, input().split())
    B.append(Point(x, y))

E = [[] for _ in range(N)]
for i, r in enumerate(R):
    for j, b in enumerate(B):
        if r.x < b.x and r.y < b.y:
            E[i].append(j)

match = [-1 for _ in range(N)]


def dfs(v: int, visited: set) -> bool:
    for u in E[v]:
        if u in visited:
            continue
        visited.add(u)

        if match[u] == -1 or dfs(match[u], visited):
            match[u] = v
            return True
    return False


def bipartite_matching():
    return sum(dfs(i, set()) for i in range(N))


print(bipartite_matching())

Solution (Python):
n = int(input())
R = [list(map(int, input().split())) for i in range(n)]
B = [list(map(int, input().split())) for i in range(n)]
B.sort(key = lambda x:x[0])
R.sort(key = lambda x:x[1], reverse = True)


ans = 0
for bx,by in B:
 
    for i in range(len(R)):
        if R[i][0] < bx and R[i][1] < by:
  
            del R[i]
            ans += 1
            break
print(ans)

Solution (Python):
def can_match(x1, y1, x2, y2):
    return x1 < x2 and y1 < y2

N = int(input())
red = []
blue = []
for i in range(N):
    x, y = map(int, input().split())
    red.append((x, y))

for i in range(N):
    x, y = map(int, input().split())
    blue.append((x, y))
    
red.sort()
ans = 0
for i in range(N - 1, -1, - 1):
    xr, yr = red[i]
    yb_min = 2 * N
    arg_yb_min = 0
    matched = False
    for xb, yb in blue:
        if can_match(xr, yr, xb, yb) and yb < yb_min:
            matched = True
            arg_yb_min = xb
            yb_min = yb
    
    if matched:
        ans += 1
        blue.remove((arg_yb_min, yb_min))

print(ans)

Solution (Python):
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from operator import itemgetter

n = ni()
ab = [tuple(li()) for i in range(n)]
cd = [tuple(li()) for i in range(n)]

ab.sort(key=itemgetter(0), reverse=True)
cd.sort(key=itemgetter(1))

cd_used = [False]*n

ans = 0
for ai, bi in ab:
    for j, (cj, dj) in enumerate(cd):
        if ai < cj and bi < dj and (not cd_used[j]):
            cd_used[j] = True
            ans += 1

            break

print(ans)

Solution (Python):
N = int(input())
L1 = [list(map(int,input().split())) for i in range(N)]
L2 = [list(map(int,input().split())) for i in range(N)]

L1 = sorted(L1,key=lambda x: x[1],reverse=True)
L2 = sorted(L2,key=lambda x: x[0],reverse=False)

ans = 0

checked = [0 for i in range(N)]

for i in range(N) :
    c,d = L2[i]
    for j in range(N) :
        a,b = L1[j]
        
        if a < c and b < d :
            if checked[j] :
                continue
            ans += 1
            checked[j] = 1
            break

print(ans)

Solution (Python):
N=int(input())
red=[]
blue=[]
for i in range(N):
    a,b=map(int,input().split())
    red.append((a,b,i))
for i in range(N):
    a,b=map(int,input().split())
    blue.append((a,b,N+i))
from collections import defaultdict
branch=defaultdict(set)
for x in red:
    for y in blue:
        if x[0]<y[0] and x[1]<y[1]:
            branch[x[2]]|={y[2]}
            branch[y[2]]|={x[2]}
def main(i):
    global used
    global branch
    global match
    if i in used:
        return False
    for nex in branch[i]:
        if nex in used:
            continue
        elif match[nex]==-1:
            match[nex]=i
            match[i]=nex
            return True
        else:
            used|={i}
            if main(match[nex]):
                match[i]=nex
                match[nex]=i
                return True
    return False
match=[-1]*(2*N)
for i in range(2*N):
    used=set()
    if match[i]==-1:
        main(i)
print(sum([1 for i in range(2*N) if match[i]>=0])//2)

Solution (Python):
import copy
N = int(input())
R = []
for i in range(N):
    a,b = map(int,input().split())
    R.append([a,b])
B = []
for i in range(N):
    c,d = map(int,input().split())
    B.append([c,d])

INF = 10**10
R.sort(key=lambda x:-x[1])
B.sort(key=lambda x:x[0])
ans = 0
for r in range(N):
    for b in range(N):
        if R[r][0]<B[b][0] and R[r][1]<B[b][1]:
            ans+=1
            B[b][0]=-INF
            B[b][1]=-INF
            break
print(ans)

Solution (Python):
N=int(input())
R=sorted([list(map(int,input().split())) for i in range(N)])
B=sorted([list(map(int,input().split())) for i in range(N)])
ans=0
for i in range(N):
    a=[-1,-1]
    x=-1
    for j in range(len(R)):
        if B[i][0]>R[j][0] and B[i][1]>R[j][1]:
            if a[1]<R[j][1]:
                a=R[j]
                x=j
    if x>=0:
        del R[x]
        ans+=1
print(ans)

Solution (Python):
n = int(input())
ab = [tuple(map(int, input().split())) for _ in range(n)]
cd = [tuple(map(int, input().split())) for _ in range(n)]
ab.sort(key=lambda x: x[1], reverse=True)
cd.sort(key=lambda x: x[0])
ans = 0

for i in cd:
    j = 0
    for j in range(len(ab)):
        if ab[j][0] < i[0] and ab[j][1] < i[1]:
            ab.pop(j)
            ans += 1
            break
print(ans)

Solution (Python):
N=int(input())
red=[tuple(map(int,input().split())) for i in range(N)]
blue=[tuple(map(int,input().split())) for j in range(N)]
from collections import defaultdict
branch=defaultdict(set)
for i,r in enumerate(red):
    for j,b in enumerate(blue):
        if r[0]<b[0] and r[1]<b[1]:
            branch[r[0]]|={b[0]}
            branch[b[0]]|={r[0]}
match=[-1]*(2*N)
used=[False]*(2*N)
def dfs(v):
    global used
    global match
    used[v]=True
    for nex in (branch[v]):
        w=match[nex]
        if (w<0 or (not used[w] and dfs(w))):
            match[nex]=v
            match[v]=nex
            return True
    return False
    
def main():
    global used
    global match
    cnt=0
    for i in range(2*N):
        if match[i]<0:
            used=[False]*(2*N)
            if dfs(i):
                cnt+=1
    return cnt
print(main())

Solution (Python):



N = int(input())
R = [list(map(int, input().rstrip().split(' '))) for _ in range(N)]
B = [list(map(int, input().rstrip().split(' '))) for _ in range(N)]
G = [[] for _ in range(2 * N + 2)]

def add_edge(f, to, cap):
    G[f].append([to, cap, len(G[to])])
    G[to].append([f, 0, len(G[f]) - 1])


for i in range(N):
    add_edge(0, 1 + i, 1)
    add_edge(N + 1 + i, 2 * N + 1, 1)
    

for i in range(N):
    for j in range(N):
        if R[i][0] < B[j][0] and R[i][1] < B[j][1]:
            add_edge(1 + i, N + 1 + j, 1)

used = [False] * (2 * N + 2)
def dfs(v, t, f):
    if v == t: return f
    used[v] = True
    for i in range(len(G[v])):
        e_to, e_cap, e_rev = G[v][i]
        if used[e_to] == False and e_cap > 0:
            d = dfs(e_to, t, min(f, e_cap))
            if d > 0:
                G[v][i][1] -= d
                G[e_to][e_rev][1] += d
                return d
    return 0
    
def max_flow(s, t):
    global used
    flow = 0
    while True:
        used = [False] * (2 * N + 2)
        f = dfs(s, t, float('inf'))
        if f == 0: return flow
        flow += f
        
print(max_flow(0, 2 * N + 1))

Solution (Python):
N = int(input())
AB = [tuple([int(_) for _ in input().split()]) for i in range(N)]
CD = [tuple([int(_) for _ in input().split()]) for i in range(N)]

AB = sorted(AB, key=lambda x:x[1])
CD = sorted(CD, key=lambda x:x[0])

result = 0

for c, d in CD:
    xs = [x for x in AB if x[0] < c and x[1] < d]
    if xs:
        AB.remove(xs[-1])
        result += 1

print(result)

Solution (Python):
n=int(input())
p1=[list(map(int,input().split())) for _ in range(n)]
p2=[list(map(int,input().split())) for _ in range(n)]
p1=sorted(p1,key=lambda x:x[1])
p1=sorted(p1,key=lambda x:x[0])
p2=sorted(p2,key=lambda x:x[1])
p2=sorted(p2,key=lambda x:x[0])
ans=0
for i in range(n-1,-1,-1):
  pos=-1
  tmp=10**10
  for j in range(len(p2)-1,-1,-1):
    if p1[i][0]>=p2[j][0]:
      break
    else:
      if p1[i][1]<p2[j][1] and p2[j][1]<tmp:
        tmp=p2[j][1]
        pos=j
  if pos!=-1:
    ans+=1
    p2.pop(pos)
print(ans)

Solution (Python):
from collections import deque

class Dinic:
    def __init__(self, v, inf=float('inf')):
        self.V = v
        self.inf = inf
        
        self.G = [[] for _ in range(self.V)]
        self.level = [0] * self.V
        self.iter = [0] * self.V

    def add_edge(self, from_v, to_v, cap):
        self.G[from_v].append({'to': to_v, 'cap': cap, 'rev': len(self.G[to_v])})
        self.G[to_v].append({'to': from_v, 'cap': 0, 'rev': len(self.G[from_v]) - 1})

    def bfs(self, s):
        self.level = [-1] * self.V
        self.level[s] = 0
        que = deque()
        que.appendleft(s)

        while len(que) > 0:
            v = que.pop()
            for i in range(len(self.G[v])):
                e = self.G[v][i]
                if e['cap'] > 0 and self.level[e['to']] < 0:
                    self.level[e['to']] = self.level[v] + 1
                    que.appendleft(e['to'])

    def dfs(self, v, t, f):
        if v == t:
            return f

        for i in range(self.iter[v], len(self.G[v])):
            self.iter[v] = i
            e = self.G[v][i]
            if e['cap'] > 0 and self.level[v] < self.level[e['to']]:
                d = self.dfs(e['to'], t, min(f, e['cap']))
                if d > 0:
                    e['cap'] -= d
                    self.G[e['to']][e['rev']]['cap'] += d
                    return d

        return 0

    def maxflow(self, s, t):
        flow = 0
        while True:
            self.bfs(s)
            if self.level[t] < 0:
                return flow

            self.iter = [0] * self.V
            f = self.dfs(s, t, self.inf)
            while f > 0:
                flow += f
                f = self.dfs(s, t, self.inf)


def read_input():
    n = int(input())

    red = []
    for i in range(n):
        a, b = map(int, input().split())
        red.append((a, b))

    blue = []
    for i in range(n):
        c, d = map(int, input().split())
        blue.append((c, d))

    return n, red, blue


def submit():
    n, red, blue = read_input()
    dinic = Dinic(2*n+2)

    for i in range(n):
        dinic.add_edge(2*n, i, 1)
        dinic.add_edge(n+i, 2*n+1, 1)

    for i,b in enumerate(blue):
        for j,r in enumerate(red):
            if r[0] < b[0] and r[1] < b[1]:
                dinic.add_edge(j, n+i, 1)

    print(dinic.maxflow(2*n, 2*n+1))

if __name__ == '__main__':
    submit()

Solution (Python):
N = int(input())
red = [list(map(int, input().split())) for _ in range(N)]
blue = [list(map(int, input().split())) for _ in range(N)]
red.sort()
blue.sort()
ret =0
for c, d in blue:
    diff = 10 ** 5
    tmp = -1
    for i, (a, b) in enumerate(red):
        if c > a and d > b:
            if diff > d-b:
                diff = d - b
                tmp = i
    if tmp > -1:
        red[tmp] = [10**5, 10**5]
        ret += 1
print(ret)

Solution (Python):
n = int(input())
reds = list()
blues = list()
counter = 0

for i in range(n):
    reds.append(list(map(int,input().split())))
    
for i in range(n):
    blues.append(list(map(int,input().split())))
    
blues = sorted(blues,key=lambda x:x[0])
reds = sorted(reds,key=lambda x:x[1],reverse=True)

for blue in blues:
    for red in reds:
        if red[0] < blue[0] and red[1] < blue[1]:
            red[1] = 201
            counter += 1
            break
print(counter)

Solution (Python):
n = int(input())

reds = []
for _ in range(n):
    reds.append(list(map(int, input().split())))

blues = []
for _ in range(n):
    blues.append(list(map(int, input().split())))

blues = sorted(blues, key=lambda x: x[0])

count = 0
for blue in blues:
    lefts = sorted([red for red in reds if red[0] < blue[0]
                    and red[1] < blue[1]], key=lambda r: r[1])
    if len(lefts) == 0:
        continue

    reds.remove(lefts[-1])
    count += 1

print(count)

Solution (Python):
n=int(input())
a=[list(map(int,input().split()))for i in range(n)]
b=[list(map(int,input().split()))for i in range(n)]
a.sort(key=lambda x:x[1],reverse=1)
b.sort(key=lambda x:x[0])
ans=0
for i in a:
  for j in b:
    if i[0]<j[0] and i[1]<j[1]:
      b.remove(j)
      ans+=1
      break
print(ans)

Solution (Python):
N = int( input() )
red_Point  = [ list( map(int, input().split()) ) for _ in range(N) ]
blue_Point = [ list( map(int, input().split()) ) for _ in range(N) ]

blue_Point.sort()

ans = 0

for i in range(N):
    index = -1
    diff  = 200
    for j in range(len(red_Point)):
        if red_Point[j][0] < blue_Point[i][0] and red_Point[j][1] < blue_Point[i][1]:
            if blue_Point[i][1] - red_Point[j][1] < diff:
                diff = blue_Point[i][1] - red_Point[j][1] 
                index = j
    if index != -1:
        del red_Point[index]
        ans += 1

print( ans )

Solution (Python):
INF = 1000000007

class Edge:
    def __init__(self, to, cap, rev):
        self.to = to
        self.cap = cap
        self.rev = rev

class FF:
    def __init__(self, n):
        self.n = n
        self.adj = []
        for i in range(n):
            self.adj.append([])
        self.used = [False] * n

    def add_edge(self, _from, _to, _cap):
        self.adj[_from].append(Edge(_to, _cap, len(self.adj[_to])))
        self.adj[_to].append(Edge(_from, 0, len(self.adj[_from]) - 1))

    def dfs(self, v, t, f):
        if v == t:
            return f
        self.used[v] = True
        for e in self.adj[v]:
            if self.used[e.to] == False and e.cap > 0:
                d = self.dfs(e.to, t, min(f, e.cap))
                if d > 0:
                 e.cap -= d
                 self.adj[e.to][e.rev].cap += d
                 return d
        return 0

    def max_flow(self, s, t):
        flow = 0
        while True:
            del self.used
            self.used = [False] * self.n
            f = self.dfs(s, t, INF)
            if f == 0:
                return flow
            flow += f


def main():
    n = int(input())
    reds = [tuple(int(s) for s in input().split()) for _ in range(n)]
    blues = [tuple(int(s) for s in input().split()) for _ in range(n)]
    ff = FF(2 * n + 2)
    for i in range(n):
        ff.add_edge(2 * n, i, 1)
        ff.add_edge(i + n, 2 * n + 1, 1)
    for i, (rx, ry) in enumerate(reds):
        for j, (bx, by) in enumerate(blues):
            if rx < bx and ry < by:
                ff.add_edge(i, j + n, 1)
    print(ff.max_flow(2 * n, 2 * n + 1))


if __name__ == '__main__':
    main()

Solution (Python):
def MI(): return map(int, input().split())
def II(): return int(input())
def IS(): return input()
def LI(): return list(map(int, input().split()))


n = II()
reds = [LI() for i in range(n)]
blues = [LI() for i in range(n)]
count = 0

blues.sort(key=lambda x: x[0])

for blue in blues:
    tmps = []
    for red in reds:
        if blue[0] > red[0]:
            tmps.append(red)
    tmps.sort(key=lambda x: x[1], reverse=True)
    for tmp in tmps:
        if blue[1] > tmp[1]:
            count += 1
            reds.pop(reds.index(tmp))
            break
print(count)

Solution (Python):
N = int(input())
red = []
for _ in range(N):
    a, b = map(int, input().split())
    red.append((b, a))
red.sort()
blue = []
for _ in range(N):
    c, d = map(int, input().split())
    blue.append((c, d))
blue.sort()
ans = 0
for c, d in blue:
    for i in range(N - 1, -1, -1):
        if red[i][1] < c and red[i][0] < d:
            ans += 1
            red[i] = (300, 300)
            break
print(ans)

Solution (Python):
N = int(input())
red = []
for _ in range(N):
    a, b = map(int, input().split())
    red.append((b, a))
red.sort()
blue = []
for _ in range(N):
    c, d = map(int, input().split())
    blue.append((c, d))
blue.sort()
ans = 0
for c, d in blue:
    for i in range(N - 1, -1, -1):
        if red[i][1] < c and red[i][0] < d:
            ans += 1
            red[i] = (300, 300)
            break
print(ans)

Solution (Python):
from bisect import bisect_left
n = int(input())
red = sorted([list(map(int, input().split())) for i in range(n)])
blue = sorted([list(map(int, input().split())) for i in range(n)])

ans = 0
for x,y, in blue:
    i = bisect_left([a[0] for a in red], x)
    tmp = sorted(red[:i], key=lambda x:x[1], reverse=True)
    for xx, yy in tmp:
        if y > yy:
            ans += 1
            red.remove([xx,yy])
            break
print(ans)

Solution (Python):
N = int(input())
L = []
for i in range(N):
  L.append(list(map(int,input().split())))
M = []
for i in range(N):
  M.append(list(map(int,input().split())))
L = sorted(L, key = lambda x:x[1])
L.reverse()
M.sort()
LT = [True]*N
MT = [True]*N
ans = 0
for i in range(N):
  curx = M[i][0]
  cury = M[i][1]
  for j in range(N):
    if LT[j] == True:
      if MT[i] == True:
        if curx >= L[j][0] and cury >= L[j][1]:
          LT[j] = False
          ans += 1
          MT[i] = False
print(ans)