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



Sample input:
3
2 0
3 1
1 3
4 2
0 4
5 5


Sample output:
2

Submission status: Accepted

Solution code:
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 x:x[1],reverse = True)
b.sort(key = lambda x:x[0])
cnt = 0
fin = []
for i in range(N):
    for j in range(N):
        if b[i][0] > r[j][0] and b[i][1] > r[j][1]:
            if not(j in fin):
                cnt += 1
                fin.append(j)
                break
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

red = []
for i in range(n):
    red.append([int(j) for j in input().split()])
red.sort(key=lambda x : x[1], reverse=True)

blue = []
for i in range(n):
    blue.append([int(j) for j in input().split()])
blue.sort()

count = 0
for b in blue:
    for r in red:
        if r[0] < b[0] and r[1] < b[1]:
            count += 1
            red.remove(r)
            break
print(count)

Submission status: Accepted

Solution code:
red = list()
blue = list()
n = int(raw_input())

for i in range(0,n):
    red.append(map(int,raw_input().split()))
for i in range(0,n):
    blue.append(map(int, raw_input().split()))

red = sorted(red, key=lambda x:x[1], reverse=True)
blue = sorted(blue, key=lambda x:x[0])

c = int()
for r in red:
    for b in blue:
        if r[0]<b[0] and r[1]<b[1]:
            blue.remove(b)
            c+=1
            break
print c

Submission status: Accepted

Solution code:
from collections import deque


def main():
    """
    ab: red
    cd: blue

    仲良しペア: 最大で何個
        赤い点の x座標が青い点の x座標より小さい
        赤い点の y座標も青い点の y座標より小さい
        1つの点が複数のペアに所属することはできません。
    """
    N = int(input())

    ab = [
        tuple(map(int, input().split()))
        for _ in range(N)
    ]
    cd = [
        tuple(map(int, input().split()))
        for _ in range(N)
    ]

    f(N, ab, cd)


def f(N, ab, cd):
    """
    sortしてみたけどダメだった
        左下にある青
        右上にある赤
    N=100
    全ペア作ってみる: TLE
        ab/cdの全並べ方の組合せ
            ab permutation N!
            cd permutation N!
            N! * N!

    左下にある赤は価値が高い
    x座標の小さい青から始めているので, 赤x座標は青xより小さいものが対象
    y座標が高いものを選ぶのは
    選定中の赤座標と次の青と比較した時,
        次の青は今の青よりxは大きい
        つまり今y選定している赤のxより必ず大きいためどのx座標であるかは気にならない
        yが小さいものとペアにしたとき,とらなかったmax_yが次の青yより大きい可能性があるため
    """
    cd = deque(sorted(cd))

    used = [False] * N
    ans = 0
    while cd:
        c, d = cd.popleft()
        ai, ay = None, -1
        for i, (a, b) in enumerate(ab):
            if used[i]:
                continue
            if a < c and b < d:
                if b > ay:
                    ai, ay = i, b

        if ai is not None:
            # sorted abに対して見つかった瞬間にペアにしていたがy判断をする
            used[ai] = True
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=input()
a=[]
for i in range(2*N):
	x,y=map(int, raw_input().split())
	if i<N: a.append([x,y,"R"])
	else: a.append([x,y,"B"])

b=sorted(a, key=lambda x: x[0], reverse=True)


q=[]
ans=0
for i in b:
	x=i[1]
	type=i[2]
	if type=="B":
		q.append(x)
	elif type=="R" and len(q)>0:
		up=0
		tmp=[]
		for j in q:
			if x<=j:
				tmp.append(j)
				up=1
		ans+=up
		if len(tmp)>0:
			val=min(tmp)
			q.remove(val)

print ans

Submission status: Accepted

Solution code:
red = list()
blue = list()
n = int(raw_input())

for i in range(0,n):
    red.append(map(int,raw_input().split()))
for i in range(0,n):
    blue.append(map(int, raw_input().split()))

red = sorted(red, key=lambda x:x[1], reverse=True)
blue = sorted(blue, key=lambda x:x[0])

c = int()
for b in blue:
    for i,r in enumerate(red):
        if r[0]<b[0] and r[1]<b[1]:
            red.pop(i)
            c+=1
            break

print c

Submission status: Accepted

Solution code:
n = int(raw_input())

red = []
blue = []

for a in range(n):
  q = map(int,raw_input().split(" "))
  red.append(q)

for a in range(n):
  q = map(int,raw_input().split(" "))
  blue.append(q)

red.sort()
blue.sort()
G = []
for a in range(n*2):
  G.append([])

used = [0]*(n*2)
match = [-1]*(n*2)

def add_edge(u,v):
  global G
  G[u].append(v)
  G[v].append(u)

def dfs(v):
  global G,match,used
  used[v] = 1
  for i in range( len(G[v]) ):
    u = G[v][i]
    w = match[u]
    if w < 0 or (used[w] != 1) and dfs(w) == 1:
      match[v] = u
      match[u] = v
      return 1
  return 0

for a in range(n):
  for b in range(n):
    if red[a][0] < blue[b][0] and red[a][1] < blue[b][1]:
      add_edge(a,b+n)

ans = 0
for a in range(n):
  if match[a] < 0:
    used = [0]*n
    if dfs(a) == 1:
      ans += 1
print ans

Submission status: Accepted

Solution code:
n=int(input())
red=[0]*n
for i in range(n):
    a,b=map(int,input().split())
    red[i]=(a,b)
blue=[0]*n
for i in range(n):
    a,b=map(int,input().split())
    blue[i]=(a,b)
blue.sort()
c=0
for bx,by in blue:
    maxry=-1
    maxi=-1
    for i in range(len(red)):
        rx,ry=red[i]
        if rx < bx and ry < by:
            if maxry < ry:
                maxry=ry
                maxi=i
    if not maxi == -1:
        c+=1
        red.pop(maxi)
print(c)

Submission status: Accepted

Solution code:

n = int(input())

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

blue_array = []
for i in range(n):
    blue_array.append([int(x) for x in input().split()])
blue_array.sort()

for blue in blue_array:
    for red in red_array:
        if blue[0] > red[0] and blue[1] > red[1]:
            red_array.remove(red)
            break

print(n - len(red_array))

Submission status: Accepted

Solution code:
from pprint import pprint
N = int(input())
reds = []
for i in range(N):
    reds.append(tuple(map(int, input().split(" "))))
blues = []
for i in range(N):
    blues.append(tuple(map(int, input().split(" "))))
reds.sort(key=lambda x: (x[1], x[0]), reverse=True)
blues.sort()
picked = [False for _ in range(N)]
ans = 0
for blue in blues:
    for i, red in enumerate(reds):
        if blue[0] > red[0] and blue[1] > red[1] and not picked[i]:
            ans += 1
            picked[i] = True
            break
print(ans)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
#    N = int(input())
#    A = []
#    B = []
#    C = []
#    D = []
#    for i in range(N):
#        buf1, buf2 = list(map(int, (input().split(' '))))
#        A.append(buf1)
#        B.append(buf2)
#    for i in range(N):
#        buf1, buf2 = list(map(int, (input().split(' '))))
#        C.append(buf1)
#        D.append(buf2)
#
#    def calc_dist(a, b, c, d):
#        return pow((pow((c - a), 2) + pow((d - b), 2)), 0.5)
#
#    A_dist_buf = {}
#    B_dist_buf = {}
#    for i in range(N):
#        a_key = str(A[i]) + '_' + str(B[i])
#        b_key = str(C[i]) + '_' + str(D[i])
#        A_dist_buf[a_key] = calc_dist(0, 0, A[i], B[i])
#        B_dist_buf[b_key] = calc_dist(0, 0, C[i], D[i])
#    A_dist = sorted(A_dist_buf.items(), key=lambda x: x[1], reverse=True)
#    B_dist = sorted(B_dist_buf.items(), key=lambda x: x[1], reverse=True)
##    print(A_dist)
##    print(B_dist)
#    A = []
#    B = []
#    C = []
#    D = []
#    for key,ele in A_dist:
#        A.append(key.split('_')[0])
#        B.append(key.split('_')[1])
#    for key,ele in B_dist:
#        C.append(key.split('_')[0])
#        D.append(key.split('_')[1])
#    ans1 = 0
#    for i in range(N):
#        for j in range(N):
#            if len(A) <= i or len(C) <= j:
#                break
#            if ((A[i] < C[j]) and (B[i] < D[j])):
##                print(A[i], B[i], C[j], D[j])
#                ans1 += 1
#                del C[j], D[j]
#                break
#    print(ans1)

    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 x: (x[1], x[0]), reverse=True)
    B.sort(key=lambda x: (x[0], x[1]))
    f = [0] * N

    pair = 0
    for b in B:
        for i, r in enumerate(R):
            if r[0] < b[0] and r[1] < b[1] and f[i] == 0:
                pair += 1
                f[i] = 1
                break

    print(pair)

Submission status: Accepted

Solution code:
from math import sqrt

N = int(input())

reds = sorted([list(map(int, input().split(' '))) for i in range(N)])
blues = sorted([list(map(int, input().split(' '))) for i in range(N)], key = lambda x: x[0])

result = 0

for i in range(N):
    max_dis = -1
    tmp_index = -1
    length = len(reds)
    for j in range(length):
        if blues[i][0] > reds[j][0] and blues[i][1] > reds[j][1]:
            dis = reds[j][1]
            if max_dis < dis:
                max_dis = dis
                tmp_index = j
    if tmp_index != -1:
        result += 1
        reds.pop(tmp_index)

print(result)

Submission status: Accepted

Solution code:
def c_2D_plane_2N_points(N, R, B):
    red = sorted(R)  # 赤い点の座標(x座標で昇順ソート)
    blue = sorted(B)  # 青い点の座標(x座標で昇順ソート)
    ans = 0
    for c, d in blue:
        # 注目している青い点より"左下"にある赤い点の座標のリスト
        r = [z for z in red if z[0] < c and z[1] < d]
        if r:  # 左下に赤い点があれば
            r.sort(key=lambda x: x[1])  # y座標で昇順ソート
            # 後の青い点のことを考えると、"より左下"にある赤い点を残したほうがよい。
            # x座標でソートしていることを考えると、なるべくy座標が小さな赤い点を
            # あとに残すほうがよい。(y座標が大きな赤い点を先にペアにする)
            red.remove(r[-1])  # r[-1]が指す赤い点とペアにする。もう選べないので削除
            ans += 1
    return ans
N = int(input())
R = [[int(i) for i in input().split()] for j in range(N)]
B = [[int(i) for i in input().split()] for j in range(N)]
print(c_2D_plane_2N_points(N, R, B))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input().strip())
    lines = [input().split() for _ in range(N)]
    ab = [(int(line[0]), int(line[1])) for line in lines]
    lines = [input().split() for _ in range(N)]
    cd = [(int(line[0]), int(line[1])) for line in lines]

    candidates = []
    count = 0

    points = sorted(ab + cd, key=lambda x: x[0])
    for p in points:
        if p in ab:
            candidates.append(p)
            candidates = sorted(candidates, key=lambda x: x[1], reverse=True)
        else:
            # all candidates.x are smaller than p.x
            # so the best pair is the candidate which has the smallest y
            # in the all candidates.
            for i, cand in enumerate(candidates):
                if cand[1] < p[1]:
                    count += 1
                    candidates.pop(i)
                    break

    print(count)

Submission status: Accepted

Solution code:
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.sort(key=lambda xy:xy[1], reverse=True)
CD.sort()
for c, d in CD:
    for a, b in AB:
        if a < c and b < d:
            AB.remove((a, b))
            break

print(N - len(AB))

Submission status: Accepted

Solution code:
n = int(raw_input())

red = []
blue = []
data = []

for a in range(n):
  q = map(int,raw_input().split(" "))
  red.append(q)
  data.append(q)

for a in range(n):
  q = map(int,raw_input().split(" "))
  blue.append(q)
  data.append(q)

red.sort()
blue.sort()
data.sort()

s = []
ans = 0

for a in data:
  if a in red:
    s.append(a)
  else:
    q = None
    y = -1
    for b in s:
      if y < b[1] and b[1] < a[1]:
        q = b
        y = b[1]
    if q != None:
      s.remove(q)
      ans += 1

print ans

Submission status: Accepted

Solution code:
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 = sorted(B, key = lambda x:x[0])
ans = 0
for b in B:
    tmp = -1
    rp = -1
    for i, r in enumerate(R):
        if b[0] > r[0] and b[1] > r[1]:
            if r[1] > tmp:
                tmp = r[1]
                rp = i
    if rp >= 0:
        R.pop(rp)
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input().strip())
ab = [list(map(int, input().strip().split(" "))) for _ in range(n)]
cd = [list(map(int, input().strip().split(" "))) for _ in range(n)]

candidate = []
ans = 0
ps = sorted(ab + cd, key=lambda x: x[0])
for p in ps:
    if p in ab:
        candidate.append(p)
        candidate = sorted(candidate, key=lambda x: x[1], reverse=True)
    else:
        for i, _p in enumerate(candidate):
            if _p[1] < p[1]:
                ans += 1
                candidate.pop(i)
                break
print(ans)

Submission status: Accepted

Solution code:
# -- coding: utf-8 --
from collections import deque
N = int(input())
red = []
for i in range(N):
    red.append(list(map(int, input().split(' '))))
blue = []
for i in range(N):
    blue.append(list(map(int, input().split(' '))))
red = deque(sorted(red, key=lambda x:(x[1], x[0]), reverse=True))
blue = deque(sorted(blue))
count = 0
flag = [0] * N
for b in blue:
    for i, r in enumerate(red):
        if r[0] < b[0] and r[1] < b[1] and flag[i] == 0:
            count += 1
            flag[i] = 1
            break

print(count)

Submission status: Accepted

Solution code:
class Graph:
    def __init__(self, graph):
        self.graph = graph # residual graph
        self.part_1 = len(graph)
        self.part_2 = len(graph[0])
    
    # A DFS based recursive function that returns true if a matching for vertex u is possible
    def bpm(self, u, matchR, visited):
        for v in range(self.part_2):
            if self.graph[u][v] and visited[v] == False:
                visited[v] = True
                if matchR[v] == -1 or self.bpm(matchR[v], matchR, visited):
                    matchR[v] = u
                    return True
        return False

    # Returns maximum number of matching 
    def maxBPM(self):
        matchR = [-1] * self.part_2
        result = 0
        for i in range(self.part_1):
            visited = [False] * self.part_2
            if self.bpm(i, matchR, visited):
                result += 1
        return result

N = int(input())
reds = [tuple(map(int, input().split())) for _ in range(N)]
blues = [tuple(map(int, input().split())) for _ in range(N)]

edges = [[0 for _ in range(N)] for _ in range(N)]

for i in range(N):
    for j in range(N):
        if reds[i][0] < blues[j][0] and reds[i][1] < blues[j][1]:
            edges[i][j] = 1

g = Graph(edges)

print(g.maxBPM())

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from collections import deque

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 x:(x[1],x[0]), reverse=True)
B.sort(key=lambda x:(x[0],x[1]))
R = deque(R)
B = deque(B)
f = [0]*N

pair = 0
for b in B:
    for i, r in enumerate(R):
        if r[0]<b[0] and r[1]<b[1] and f[i] == 0:
            pair += 1
            f[i] = 1
            break
    
print(pair)

Submission status: Accepted

Solution code:
N = int(input())

a, b = [], []
for n in range(N):
    _a, _b = map(int, input().split(' '))
    a.append(_a)
    b.append(_b)

c, d = [], []
for n in range(N):
    _c, _d = map(int, input().split(' '))
    c.append(_c)
    d.append(_d)

def dfs(v, visited):
    """
    :param v: X側の未マッチングの頂点の1つ
    :param visited: 空のsetを渡す（外部からの呼び出し時）
    :return: 増大路が見つかればTrue
    """
    for u in edges[v]:
        if u in visited:
            continue
        visited.add(u)
        if matched[u] == -1 or dfs(matched[u], visited):
            matched[u] = v
            return True
    return False

# 標準入力からのグラフ読み取り
edges = [set() for _ in range(N)]
matched = [-1] * N

# 隣接行列
M = [[0 for _ in range(N)] for _ in range(N)]
for i in range(N):
    for j in range(N):
        if a[i] < c[j] and b[i] < d[j]:
            edges[i].add(j)

# 増大路発見に成功したらTrue(=1)。合計することでマッチング数となる
print(sum(dfs(s, set()) for s in range(N)))

Submission status: Accepted

Solution code:
from collections import defaultdict
n = int(input())
G = defaultdict(list)
rec = []
for i in range(n):
    a, b = map(int, input().split())
    rec.append((i, a, b))


for i in range(n):
    c, d = map(int, input().split())
    for index, p, q in rec:
        if p < c and q < d:
            G[index].append(i + n)
            G[i + n].append(index)


used = [False] * 2 * n
match = [-1] * 2 * n


def dfs(v):
    global used
    used[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not used[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True


ma = 0
for i in range(n):
    if match[i] < 0:
        used = [False] * 2 * n
        if dfs(i):
            ma += 1

print(ma)

Submission status: Runtime Error

Solution code:
N = int(input())
reds = sorted([list(map(int,input().split())) for i in range(n)])
blues = sorted([list(map(int,input().split())) for i in range(n)])

reds = reds[::-1]
res = 0

for c,d in blues:
  for a,b in reds:
    if a<c and b<d:
      reds.remove([a,b])
      res+=1
      break
      
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
reds = sorted([list(map(int,input().split())) for i in range(n)])
blues = sorted([list(map(int,input().split()))])

reds = reds[::-1]
res = 0

for c,d in blues:
  for a,b in reds:
    if a<c and b<d:
      reds.remove([a,b])
      res+=1
      break
      
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
reds = sorted([list(map(int,input().split()))])
blues = sorted([list(map(int,input().split()))])

reds.sort(reverse = True)
res = 0

for c,d in blues:
  for a,b in reds:
    if a,c and b<d:
      reds.remove([a,b])
      res+=1
      break
      
print(res)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import networkx as nx
from networkx.algorithms import bipartite
import numpy as np
import itertools
import matplotlib.pyplot as plt


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


match_list = [[] for i in range(n)]
for i in range(n):
    for j in range(n):
        a, b = ab[i]
        c, d = cd[j]

        if a < c and b < d:
            match_list[i].append(j)
# print(match_list)


group1 = range(n)
group2 = range(n, 2*n)

g = nx.Graph()
g.add_nodes_from(group1, bipartite=1)
g.add_nodes_from(group2, bipartite=0)

for i, list_ in enumerate(match_list):
    for j in list_:
        g.add_edge(i, j+n, weight=1)

# A, B = bipartite.sets(g)
# print(A, B)
# pos = dict()
# pos.update((n, (1, i)) for i, n in enumerate(A))
# pos.update((n, (2, i)) for i, n in enumerate(B))
# nx.draw_networkx(g, pos)
# nx.draw_networkx_edges(g, pos)
# plt.axis("off")
# plt.show()


d = nx.max_weight_matching(g)

print(len(d))

Submission status: Runtime Error

Solution code:
N = int(input())
Red = sorted([tuple(map(int, input().split())) for _ in range(N)])
Blue = sorted([tuple(map(int, input().split())) for _ in range(N)])

ans = 0
switch = lambda X:(X[1],X[0])
for b in Blue:
    C = [switch(r) for r in Red if r < b] #候補
    if len(C) == 0:
        continue
    ans += 1
    C.sorted(reverse=True)
    Red.remove(switch(C[0]))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
Red = sorted([tuple(map(int, input().split())) for _ in range(N)])
Blue = sorted([tuple(map(int, input().split())) for _ in range(N)])

ans = 0
for b in Blue:
    C = [r for r in Red if r < b] #候補
    if len(C) == 0:
        continue
    ans += 1
    pair = (0,0)
    for c in C:
        if c[1] > pair[1]:
            pair = c
    Red.remove(pair)
print(ans)

Submission status: Runtime Error

Solution code:

n = int(input().strip())
red = [list(map(int, input().split())) for i in range(n)]
blue = [list(map(int, input().split())) for j in range(n)]

for i in range(n):
    '''
    kagi_r = [min(red[i][0], red[i][1]), max(red[i][0], red[i][1])]
    kagi_b = [min(blue[i][0], blue[i][1]), max(blue[i][0], blue[i][1])]
    red[i] = red[i] + kagi_r
    blue[i] = blue[i] + kagi_b
    '''
red.sort(key=lambda x: (x[0], x[1]), reverse=True)
blue.sort(key=lambda x: (x[0], x[1]))
flg_r = [0]*n
cnt = 0

for i in range(n):
    for j in range(n):
        if flg_r[j]:
            continue
        if red[j][0] < blue[i][0] and red[j][1] < blue[i][1]:
            flg_a[j] = 1
            cnt += 1
            break

print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
red = [list(map(int, input().split())) for _ in range(N)]
blue = [list(map(int, input().split())) for _ in range(N)]

# x座標でソート
blue.sort(key=lambda x: x[1])

# y座標で降順ソート
red.sort(reverse=True)

# print(blue)
# print(red)

# blue_list = [0] * N
red_list = [0] * N


for b in range(N):
    for r in range(N):
        if red_list[r] == 1:
            continue
        if red[r][0] < blue[b][0] and red[r][1] < blue[b][1]:
            # print(b, r)
            # blue_list[b] = 1
            red_list[r] = 1
            break

print(sum(blue_list))

Submission status: Runtime Error

Solution code:
n = int(input())
red  = [tuple(map(int, input().split())) for _ in range(n)]
blue = [tuple(map(int, input().split())) for _ in range(n)]
red.sort(key=lambda x:-x[1])
blue.sort()
count = 0
for xb, yb in blue:
    for xr, yr in red:
        if xb > xr and yb > yr:
            red[max_i] = (201, 201)
            count += 1
            break
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
ab = [[int(i) for i in input().split()] for _ in range(N)]
cd = [[int(i) for i in input().split()] for _ in range(N)]

ab.sort(key = lambda x: x[1], reverse=True)
cd.sort()

a = 0
for c, d in cd:
    for a, b in ab:
        if a < c and b < d:
            a += 1
            ab.remove([a, b])
            break

print(a)

Submission status: Runtime Error

Solution code:
N=int(input())
R=[list(map(int,input().split())) for n in range(N)]
B=[list(map(int,input().split())) for n in range(N)]

sorted(R,key=itemgetter(1),reverse=True)
B.sort()
ans=0

for c,d in B :
    for a,b in R :
        if a<c and b<d :
            ans+=1
            R.remove([a,b])
            break

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

class Dinic:
    def __init__(self, len_X, len_Y):
        self.len_X, self.len_Y = len_X, len_Y
        self.edges = [[] for _ in range(len_X)]
        self.matched = [-1] * len_Y
        self.visited = set()

    def add_edge(self, x, y):
        self.edges[x].append(y)

    def dfs(self, x):
        """
        :param x: X側の未マッチングの頂点の1つ
        :param visited: 空のsetを渡す（外部からの呼び出し時）
        :return: 増大路が見つかればTrue
        """
        for next_x in self.edges[x]:
            if next_x in self.visited:
                continue
            self.visited.add(next_x)
            if self.matched[next_x] == -1 or self.dfs(self.matched[next_x]):
                self.matched[next_x] = x
                return True
        return False

    def solve(self):
        """ 増大路発見に成功したらTrue(=1)。合計することでマッチング数となる """
        res = 0
        for x in range(self.len_X):
            self.visited = set()
            res += self.dfs(x)
        return res

#################################################################################################


len_X, len_Y, E = map(int, input().split())     # len_X, len_Y: 部分集合に含まれる頂点の数　E: 辺の数


dinic = Dinic(len_X, len_Y)

for _ in range(E):
    x, y = map(int, input().split())
    dinic.add_edge(x,y)

print(dinic.solve())

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc091/tasks/arc092_a

import sys
input = sys.stdin.readline
from collections import deque


class MaxFlow:
    class Edge:
        def __init__(self, to, cap, rev):
            """
            :param to:  終点ノード
            :param cap: 残された容量
            :param rev: 遂になる逆向きのエッジ（ノード to の rev 番目のエッジ）
            """
            self.to, self.cap, self.rev = to, cap, rev

    def __init__(self, node_size, inf):
        self._node = node_size
        self._inf = inf
        self._level = [-1] * self._node
        self._iter = [0] * self._node
        self._graph = [[] for _ in range(self._node)]

    def add_edge(self, from_, to, cap):
        self._graph[from_].append(self.Edge(to, cap, len(self._graph[to])))
        self._graph[to].append(self.Edge(from_, 0, len(self._graph[from_]) - 1))

    def bfs(self, start):
        """
        _level: 未訪問の場合は -1, 訪問済みの場合は start からの最短距離（正容量のエッジのみを使った経路の中での）
        """
        self._level = [-1] * self._node
        next_set = deque()
        self._level[start] = 0
        next_set.append(start)
        while next_set:
            cur_vertex = next_set.popleft()
            for edge in self._graph[cur_vertex]:
                if self._level[edge.to] < 0 < edge.cap:
                    self._level[edge.to] = self._level[cur_vertex] + 1
                    next_set.append(edge.to)

    def dfs(self, cur_vertex, end_vertex, flow):
        if cur_vertex == end_vertex:
            return flow
        while self._iter[cur_vertex] < len(self._graph[cur_vertex]):
            edge = self._graph[cur_vertex][self._iter[cur_vertex]]
            if edge.cap > 0 and self._level[cur_vertex] < self._level[edge.to]:
                flowed = self.dfs(edge.to, end_vertex, min(flow, edge.cap))
                if flowed > 0:
                    edge.cap -= flowed
                    self._graph[edge.to][edge.rev].cap += flowed
                    return flowed
            self._iter[cur_vertex] += 1
        return 0

    def solve(self, source, sink):
        flow = 0
        while True:
            self.bfs(source)
            if self._level[sink] < 0:
                """ sinkへの最短距離が -1 ⇒ sink にたどり着けない ⇒ 増加路が存在しないため探索終了"""
                return flow
            self._iter = [0] * self._node
            while True:
                f = self.dfs(source, sink, self._inf)
                if f == 0:
                    break
                flow += f


class Dinic:
    def __init__(self, len_X, len_Y):
        """
        soursの位置 = 0
        sinkの位置  = len_X + len_Y + 1
        capacity   = 1
        の最大フロー問題
        """
        self.len_X, self.len_Y = len_X, len_Y
        self.mf = MaxFlow(self.len_X + self.len_Y + 2, min(self.len_X, self.len_Y))
        for i in range(self.len_X):
            """ sours から部分集合Xへの各点に capacity=1 の流れを作る """
            self.mf.add_edge(0, i + 1, 1)
        for i in range(self.len_Y):
            """ 部分集合Yへの各点からsinkへの capacity=1 の流れを作る """
            self.mf.add_edge(self.len_X + i + 1, self.len_X + self.len_Y + 1, 1)

    def add_edge(self, from_, to):
        """
        :param from_: 部分集合Xの頂点　（1,...,len_X）
        :param to:    部分集合Yの頂点　（len_X + 1,...,len_X + 1 + len_Y）
        """
        self.mf.add_edge(from_ + 1, to + self.len_X + 1, 1)

    def solve(self):
        return self.mf.solve(0, self.len_X + self.len_Y + 1)

#####################################################################################

N = int(input())

X = []
Y = []
for i in range(N):
    X.append([int(i) for i in input().split()])

for i in range(N):
    Y.append([int(i) for i in input().split()])

dinic = Dinic(len(X), len(Y))

for i in range(len(X)):
    for j in range(N):
        if X[i][0] < Y[j][0] and X[i][1] < Y[j][1]:
            dinic.add_edge(i,j)


print(dinic.flow())

Submission status: Runtime Error

Solution code:
# input
N, M = map(int, input().split())

# check
if 2 in [M, N] or N == M == 1:
    print(0)
elif 1 in [M, N]:
    print(N * M - 2)
else:
    print(N * M - (2 * (N + M) - 4))

Submission status: Runtime Error

Solution code:
# input
N, M = map(int, input().split())

# check
if 2 in [M, N] or (N, M) == (1, 1):
    print(0)
elif 1 in [M, N]:
    print(N * M - 2)
else:
    print(N * M - (2 * (N + M) - 4))

Submission status: Runtime Error

Solution code:
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()
 
p=0
for bb in blue:
  lis=[-1,-1]
  for rr in red:
    if rr[0]<bb[0] and rr[1]<bb[1] and lis[1]<rr[1]:
      lis=rr
    if lis!=[-1,-1]:
      red.remove(lis)
      p+=1
print(p)

Submission status: Runtime Error

Solution code:
n = int(input())
red = {tuple(int(i) for i in input().split()): False for _ in range(n)}
blue = {tuple(int(i) for i in input().split()): False for _ in range(n)}
c = 0
for b in sorted(blue.keys()):
    for r in sorted(red.keys(), key=itemgetter(1), reverse=True):
        if not red[r] and b[0] > r[0] and b[1] > r[1]:
            red[r] = True
            blue[b] = True
            c += 1
            break
print(c)

Submission status: Runtime Error

Solution code:
N = input()
red = sorted([list(map(int,input().split())) for i in range(N)], key=lambda x: x[0])[::-1]
blue = [list(map(int,input().split())) for i in range(N)]
ans = 0
for i in range(N):
    min_ = 10 ** 9 + 7
    ind = -1
    for j in range(N):
        if red[i][0] <= blue[j][0] and red[i][1] <= blue[j][1] and blue[j][1] < min_:
            ind = j
            min_ = blue[j][1]
    if ind != -1:
        blue[ind][0] = -1
        ans += 1
print(ans)