Hide and Seek (Codeforces 1162C)
Tags: Implement, graphs

Alice and Bob are playing a game on a line with $$$n$$$ cells. There are $$$n$$$ cells labeled from $$$1$$$ through $$$n$$$. For each $$$i$$$ from $$$1$$$ to $$$n-1$$$, cells $$$i$$$ and $$$i+1$$$ are adjacent.Alice initially has a token on some cell on the line, and Bob tries to guess where it is. Bob guesses a sequence of line cell numbers $$$x_1, x_2, \ldots, x_k$$$ in order. In the $$$i$$$-th question, Bob asks Alice if her token is currently on cell $$$x_i$$$. That is, Alice can answer either "YES" or "NO" to each Bob's question.At most one time in this process, before or after answering a question, Alice is allowed to move her token from her current cell to some adjacent cell. Alice acted in such a way that she was able to answer "NO" to all of Bob's questions.Note that Alice can even move her token before answering the first question or after answering the last question. Alice can also choose to not move at all.You are given $$$n$$$ and Bob's questions $$$x_1, \ldots, x_k$$$. You would like to count the number of scenarios that let Alice answer "NO" to all of Bob's questions. Let $$$(a,b)$$$ denote a scenario where Alice starts at cell $$$a$$$ and ends at cell $$$b$$$. Two scenarios $$$(a_i, b_i)$$$ and $$$(a_j, b_j)$$$ are different if $$$a_i \neq a_j$$$ or $$$b_i \neq b_j$$$.

Input: The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq n,k \leq 10^5$$$) — the number of cells and the number of questions Bob asked.The second line contains $$$k$$$ integers $$$x_1, x_2, \ldots, x_k$$$ ($$$1 \leq x_i \leq n$$$) — Bob's questions.

Output: Print a single integer, the number of scenarios that let Alice answer "NO" to all of Bob's questions.

Note: The notation $$$(i,j)$$$ denotes a scenario where Alice starts at cell $$$i$$$ and ends at cell $$$j$$$.In the first example, the valid scenarios are $$$(1, 2), (2, 1), (2, 2), (2, 3), (3, 2), (3, 3), (3, 4), (4, 3), (4, 5)$$$. For example, $$$(3,4)$$$ is valid since Alice can start at cell $$$3$$$, stay there for the first three questions, then move to cell $$$4$$$ after the last question. $$$(4,5)$$$ is valid since Alice can start at cell $$$4$$$, stay there for the first question, the move to cell $$$5$$$ for the next two questions. Note that $$$(4,5)$$$ is only counted once, even though there are different questions that Alice can choose to do the move, but remember, we only count each pair of starting and ending positions once.In the second example, Alice has no valid scenarios.In the last example, all $$$(i,j)$$$ where $$$|i-j| \leq 1$$$ except for $$$(42, 42)$$$ are valid scenarios.

Sample Inputs:
5 3
5 1 4

4 8
1 2 3 4 4 3 2 1

100000 1
42

Sample Outputs:
9

0

299997

Solution (Python):
import sys

class Main:

    def __init__(self):
        self.buff = None
        self.index = 0

    def next(self):
        if self.buff is None or self.index == len(self.buff):
            self.buff = self.next_line()
            self.index = 0
        val = self.buff[self.index]
        self.index += 1
        return val

    def next_line(self):
        return sys.stdin.readline().split()

    def next_ints(self):
        return [int(x) for x in sys.stdin.readline().split()]

    def next_int(self):
        return int(self.next())

    def solve(self):
        (n, k) = self.next_ints()
        x = self.next_ints()
        cc = [0 for _ in range(0, n + 10)]
        us = [0 for _ in range(0, n + 10)]
        for xx in x:
            cc[xx] += 1
        res = n * 3 - 2
        for xx in x:
            cc[xx] -= 1
            if us[xx] != 0:
                continue
            res -= 1
            us[xx] = 1
            if cc[xx + 1] >= 1:
                res -= 1
            if cc[xx - 1] >= 1:
                res -= 1
        print(res)
if __name__ == '__main__':
    Main().solve()

Solution (Python):
from collections import defaultdict, deque
from heapq import heappush, heappop
from math import inf
ri = lambda : map(int, input().split())

def solve():
    (n, k) = ri()
    fst = [inf] * n
    lst = [-inf] * n
    A = list(ri())
    for i in range(k):
        x = A[i]
        x -= 1
        fst[x] = min(fst[x], i)
        lst[x] = i
    ans = 0
    for i in range(n):
        if fst[i] == inf:
            ans += 1
        if i > 0 and (fst[i] == inf or fst[i] > lst[i - 1]):
            ans += 1
        if i < n - 1 and (fst[i] == inf or fst[i] > lst[i + 1]):
            ans += 1
    print(ans)
t = 1
while t:
    t -= 1
    solve()

Solution (Python):
(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = set()
res = (n - 2) * 2 + n + 2 - len(set(a))
used1 = set()
used2 = set()
for i in range(k - 1, -1, -1):
    if a[i] - 1 in b and a[i] not in used1:
        res -= 1
        used1.add(a[i])
    if a[i] + 1 in b and a[i] not in used2:
        res -= 1
        used2.add(a[i])
    b.add(a[i])
print(res)