Maximize the Intersections (Codeforces 1552C)
Tags: Greedy, geometry

On a circle lie $$$2n$$$ distinct points, with the following property: however you choose $$$3$$$ chords that connect $$$3$$$ disjoint pairs of points, no point strictly inside the circle belongs to all $$$3$$$ chords. The points are numbered $$$1, \, 2, \, \dots, \, 2n$$$ in clockwise order.Initially, $$$k$$$ chords connect $$$k$$$ pairs of points, in such a way that all the $$$2k$$$ endpoints of these chords are distinct.You want to draw $$$n - k$$$ additional chords that connect the remaining $$$2(n - k)$$$ points (each point must be an endpoint of exactly one chord).In the end, let $$$x$$$ be the total number of intersections among all $$$n$$$ chords. Compute the maximum value that $$$x$$$ can attain if you choose the $$$n - k$$$ chords optimally.Note that the exact position of the $$$2n$$$ points is not relevant, as long as the property stated in the first paragraph holds.

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le n$$$) — half the number of points and the number of chords initially drawn.Then $$$k$$$ lines follow. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i, \, y_i \le 2n$$$, $$$x_i \ne y_i$$$) — the endpoints of the $$$i$$$-th chord. It is guaranteed that the $$$2k$$$ numbers $$$x_1, \, y_1, \, x_2, \, y_2, \, \dots, \, x_k, \, y_k$$$ are all distinct.

Output: For each test case, output the maximum number of intersections that can be obtained by drawing $$$n - k$$$ additional chords.

Note: In the first test case, there are three ways to draw the $$$2$$$ additional chords, shown below (black chords are the ones initially drawn, while red chords are the new ones):  We see that the third way gives the maximum number of intersections, namely $$$4$$$.In the second test case, there are no more chords to draw. Of course, with only one chord present there are no intersections.In the third test case, we can make at most one intersection by drawing chords $$$1-3$$$ and $$$2-4$$$, as shown below:  

Sample Inputs:
4
4 2
8 2
1 5
1 1
2 1
2 0
10 6
14 6
2 20
9 10
13 18
15 12
11 7

Sample Outputs:
4
0
1
14

Solution (Python):
import sys
input = sys.stdin.readline
from types import GeneratorType

def bootstrap(f, stack=[]):

    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to
    return wrappedfunc

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

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    (n, k) = readInts()
    g = [-1] * (2 * n + 1)
    for _ in range(k):
        (x, y) = readInts()
        g[x] = y
        g[y] = x
    p = [i for i in range(1, 2 * n + 1) if g[i] == -1]
    for i in range(n - k):
        g[p[i]] = p[i + n - k]
        g[p[i + n - k]] = p[i]
    ans = 0
    exp = [0] * (2 * n + 1)
    for i in range(1, 2 * n + 1):
        (a, b) = (g[i], i)
        if exp[a]:
            continue
        exp[a] = exp[b] = 1
        if a > b:
            (b, a) = (a, b)
        for j in range(a + 1, b):
            if a < g[j] < b:
                continue
            ans += 1
    return ans // 2
for _ in range(int(input())):
    print(solve())

Solution (Python):
def main():
    _ = int(input())
    for _ in range(_):
        ism = []
        g = []
        (n, m) = map(int, input().split())
        vis = [False] * 205
        for i in range(m):
            (ex, wy) = map(int, input().split())
            if ex > wy:
                (ex, wy) = (wy, ex)
            ism.append((ex, wy))
            vis[ex] = vis[wy] = True
        for i in range(1, 2 * n + 1):
            if not vis[i]:
                g.append(i)
            vis[i] = False
        for i in range(len(g) // 2):
            j = i + len(g) // 2
            ism.append((g[i], g[j]))
        ism.sort()
        riz = 0
        for i in range(n):
            for j in range(i + 1, n):
                if ism[j][0] < ism[i][1] and ism[j][1] > ism[i][1]:
                    riz += 1
        print(riz)
if __name__ == '__main__':
    main()

Solution (Python):
def solve():
    (n, k) = map(int, input().split())
    segments = list()
    color = [[i, False] for i in range(2 * n + 1)]
    for _ in range(k):
        (x, y) = map(int, input().split())
        segments.append((min(x, y), max(x, y)))
        color[x][1] = True
        color[y][1] = True
    free = [x[0] for x in color[1:] if not x[1]]
    d = len(free) // 2
    for i in range(d):
        segments.append((free[i], free[i + d]))
    result = 0
    for i in range(len(segments) - 1):
        a = segments[i][0]
        b = segments[i][1]
        for j in range(i + 1, len(segments)):
            c = segments[j][0]
            d = segments[j][1]
            if a < c < b < d or c < a < d < b:
                result += 1
    print(result)

def main():
    t = int(input())
    for _ in range(t):
        solve()
main()