Hide and Seek (Codeforces 1161A)
Tags: Others, 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):
def fun():
    sum = 0
    (n, k) = map(int, input().strip().split())
    (L, R) = ([0] * (n + 1), [0] * (n + 1))
    arr = input(' ')
    ls = [int(i) for i in arr.split()]
    for (i, x) in enumerate(ls):
        if not L[x]:
            L[x] = i + 1
        R[x] = i + 1
    for i in range(1, n + 1):
        if not L[i]:
            sum += 2 if i == 1 or i == n else 3
        else:
            if i > 1 and R[i - 1] < L[i]:
                sum += 1
            if i < n and R[i + 1] < L[i]:
                sum += 1
    print(sum)
if __name__ == '__main__':
    fun()

Solution (Python):
import math
import sys
import heapq
from typing import List

def traverse(mat: List[List[int]], a: int, b: int, n: int, m: int):
    q = [[a, b]]
    index = 0
    moves = [[1, 0], [-1, 0], [0, 1], [0, -1]]
    while index < len(q):
        i = q[index][0]
        j = q[index][1]
        mat[i][j] = True
        for t in moves:
            if not (i + t[0] < 0 or j + t[1] < 0 or i + t[0] >= n or (j + t[1] >= m) or mat[i + t[0]][j + t[1]]):
                q.append([i + t[0], j + t[1]])
        index += 1

def aux(v: int, g: List[List[int]], diff: List[List[int]], res: List[int]) -> int:
    temp_diff = 0
    for u in g[v]:
        temp_diff += aux(u, g, diff, res)
    if diff[v][0] <= temp_diff:
        return min(temp_diff, diff[v][1])
    res[0] += 1
    return diff[v][1]

def dijk(graph: List[List[int]], v: int, d: List[int], g: List[dict], deg: List[int]):
    d[v] = 0
    s = [False] * len(d)
    arr = [[0, v]]
    heapq.heapify(arr)
    while len(arr) > 0:
        temp = arr.pop()
        u = temp[1]
        dist = -1 * temp[0]
        if s[u]:
            continue
        s[u] = True
        for t in graph[u]:
            deg[t] -= g[t][u]
            if s[t] is False and deg[t] + dist + 1 < d[t]:
                d[t] = deg[t] + dist + 1
                heapq.heappush(arr, [-d[t], t])

def total(arr: List[int]) -> int:
    res = 0
    last = 0
    for a in arr:
        temp = last // a + 1
        res += temp
        last = a * temp
    return res

def solve():
    (n, k) = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    first = [k + 1] * (n + 2)
    last = [-1] * (n + 2)
    first[0] = -1
    first[n + 1] = -1
    last[0] = k + 1
    last[n + 1] = k + 1
    for (index, x) in enumerate(a):
        first[x] = min(first[x], index)
        last[x] = max(last[x], index)
    res = 0
    for i in range(n + 1)[1:]:
        if first[i] == k + 1:
            res += 3 - int(i == 1 or i == n)
            continue
        res += int(last[i - 1] < first[i]) + int(last[i + 1] < first[i])
    print(res)

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

Solution (Python):
(L, R) = ([0] * (100000 + 1), [0] * (100000 + 1))
ls = [0] * 100001

def fun():
    sum = 0
    (n, k) = map(int, input().strip().split())
    arr = input(' ')
    ls = [int(i) for i in arr.split()]
    for (i, x) in enumerate(ls):
        if not L[x]:
            L[x] = i + 1
        R[x] = i + 1
    for i in range(1, n + 1):
        if not L[i]:
            sum += 2 if i == 1 or i == n else 3
        else:
            if i > 1 and R[i - 1] < L[i]:
                sum += 1
            if i < n and R[i + 1] < L[i]:
                sum += 1
    print(sum)
if __name__ == '__main__':
    fun()