Virus (Codeforces 1704C)
Tags: Greedy, sortings

There are $$$n$$$ houses numbered from $$$1$$$ to $$$n$$$ on a circle. For each $$$1 \leq i \leq n - 1$$$, house $$$i$$$ and house $$$i + 1$$$ are neighbours; additionally, house $$$n$$$ and house $$$1$$$ are also neighbours.Initially, $$$m$$$ of these $$$n$$$ houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently.Every day, the following things happen in order:  Cirno chooses an uninfected house, and protect it permanently.  All uninfected, unprotected houses which have at least one infected neighbor become infected. Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect.Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever.

Input: The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Description of test cases follows.The first line of each test case consists of two positive integers $$$n, m$$$ ($$$5 \leq n \leq 10^9$$$, $$$1 \leq m \leq \min(n, 10^5)$$$) — the number of houses on the circle, and the number of houses that are initially infected. The second line of each test case consists of $$$m$$$ distinct positive integers $$$a_1, a_2, \cdots , a_m$$$ ($$$1 \leq a_i \leq n$$$) — the indices of the houses infected initially.It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case, output an integer on a separate line, which is the minimum number of infected houses in the end.

Note: In the first test case:At the start of the first day, house $$$3$$$, $$$6$$$, $$$8$$$ are infected. Choose house $$$2$$$ to protect.At the start of the second day, house $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$, $$$8$$$, $$$9$$$ are infected. Choose house $$$10$$$ to protect.At the start of the third day, no more houses are infected.In the second test case:At the start of the first day, house $$$2$$$, $$$5$$$ are infected. Choose house $$$1$$$ to protect.At the start of the second day, house $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$ are infected. No more available houses can be protected.

Sample Inputs:
8
10 3
3 6 8
6 2
2 5
20 3
3 7 12
41 5
1 11 21 31 41
10 5
2 4 6 8 10
5 5
3 2 5 4 1
1000000000 1
1
1000000000 4
1 1000000000 10 16

Sample Outputs:
7
5
11
28
9
5
2
15

Solution (Python):
import sys
import copy
import heapq
import bisect
import functools
from math import *
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import groupby
import itertools
from itertools import accumulate
from decimal import Decimal, ROUND_HALF_UP
readline = lambda : sys.stdin.readline().rstrip()

def solve():
    (n, m) = map(int, readline().split())
    fec = sorted(list(map(int, readline().split())))
    fec.append(fec[0] + n)
    dis = []
    for i in range(len(fec) - 1):
        dis.append(fec[i + 1] - fec[i] - 1)
    dis.sort(reverse=True)
    (r, d) = (0, 0)
    for x in dis:
        if x - d * 2 > 0:
            r += max(1, x - d * 2 - 1)
        else:
            break
        d += 2
    print(n - r)

def main():
    t = 1
    t = int(readline())
    for i in range(t):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
t = 1
t = int(input())
for _ in range(t):
    (n, q) = map(int, input().split())
    a = sorted(list(map(int, input().split())))
    l = [a[i] - a[i - 1] for i in range(q)]
    l[0] += n
    l.sort(reverse=True)
    (ans, i) = (0, 0)
    flag = 1
    while i < q and flag and (l[i] - 4 * i - 2 >= 0):
        ans += max(1, l[i] - 4 * i - 2)
        flag = l[i] - 4 * i - 2 > 0
        i += 1
    print(n - ans)

Solution (Python):
t = int(input())
for _ in range(t):
    (n, m) = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()
    h = []
    for i in range(1, m):
        h.append(a[i] - a[i - 1] - 1)
    h.append(n - a[-1] + (a[0] - 1))
    h.sort(reverse=True)
    day = 0
    un = 0
    for i in h:
        j = i - day * 2
        if j <= 0:
            break
        if j == 1 or j == 2:
            un += 1
        else:
            un += j - 1
        day += 2
    print(n - un)