Sakuzyo - Imprinting

A.R.C. Markland-N is a tall building with $n$ floors numbered from $1$ to $n$. Between each two adjacent floors in the building, there is a staircase connecting them.

It's lunchtime for our sensei Colin "ConneR" Neumann Jr, and he's planning for a location to enjoy his meal.

ConneR's office is at floor $s$ of the building. On each floor (including floor $s$, of course), there is a restaurant offering meals. However, due to renovations being in progress, $k$ of the restaurants are currently closed, and as a result, ConneR can't enjoy his lunch there.

CooneR wants to reach a restaurant as quickly as possible to save time. What is the minimum number of staircases he needs to walk to reach a closest currently open restaurant.

Please answer him quickly, and you might earn his praise and even enjoy the lunch with him in the elegant Neumanns' way!


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases in the test. Then the descriptions of $t$ test cases follow.

The first line of a test case contains three integers $n$, $s$ and $k$ ($2 \le n \le 10^9$, $1 \le s \le n$, $1 \le k \le \min(n-1, 1000)$) — respectively the number of floors of A.R.C. Markland-N, the floor where ConneR is in, and the number of closed restaurants.

The second line of a test case contains $k$ distinct integers $a_1, a_2, \ldots, a_k$ ($1 \le a_i \le n$) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of $k$ over all test cases does not exceed $1000$.


-----Output-----

For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor $s$ to a floor with an open restaurant.


-----Example-----
Input
5
5 2 3
1 2 3
4 3 3
4 1 2
10 2 6
1 2 3 4 5 7
2 1 1
2
100 76 8
76 75 36 67 41 74 10 77

Output
2
0
4
0
2



-----Note-----

In the first example test case, the nearest floor with an open restaurant would be the floor $4$.

In the second example test case, the floor with ConneR's office still has an open restaurant, so Sensei won't have to go anywhere.

In the third example test case, the closest open restaurant is on the $6$-th floor.

Difficulty: interview

Starter code:

Solutions:
t=int(input())
for i in range(t):
    n,s,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    s=s-1
    for i in range(n):
        if (s-i)>-1:
            if not (s-i+1) in a:
                print(i)
                break
        if (s+i)<n:
            if not (s+i+1) in a:
                print(i)
                break


import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
    n,s,k=list(map(int,input().split()))
    it=list(map(int,input().split()))
    it=set(it)
    
    for i in range(n):
        if s+i<=n and s+i not in it:
            ans=i
            break
        if s-i>=1 and s-i not in it:
            ans=i
            break
    print(ans)


t = int(input())
for y in range(t):
	n,s,k = list(map(int,input().split()))
	a = list(map(int,input().split()))
	ans = 0
	d = dict()
	for i in range(k):
		d[a[i]] = 1
	while(True):
		if(s-ans > 0 and s-ans not in d):
			break
		if(s + ans <= n and s+ans not in d):
			break
		ans += 1
	print(ans)


t = int(input())
for i in range(t):
    n, s, k = list(map(int, input().split()))
    k = list(map(int, input().split()))
    l = dict()
    for i in k:
        l[i] = 1
    
    for i in range(1005):
        i1 = s + i
        i2 = s - i
        if (i2 > 0):
            if i2 not in l:
                print(i)
                break
        if (i1 <= n):
            if i1 not in l:
                print(i)
                break
        


for i in range(int(input())):
    n, s, k = [int(i) for i in input().split()]
    l = set([int(i) for i in input().split()])
    i = 0
    res = 0
    while (1):
        if (s + i <= n and s + i not in l):
            break
        if (s - i >= 1 and s - i not in l):
            break
        i += 1
        res += 1
    print(res)

t = int(input())
for _ in range(t):
    n, s, k = list(map(int, input().split()))
    c = set(map(int, input().split()))
    l, u = s, s
    for i in range(10000):
        if (l not in c) or (u not in c):
            print(i)
            break
        if l > 1:
            l -= 1
        if u < n:
            u += 1


def main():
    for _ in range(int(input())):
        n, s, k = map(int,input().split())
        a = list(map(int,input().split()))
        
        if s not in a:
            print(0)
            continue
        
        ans = 1
        t1 = s + 1
        t2 = s - 1
        for i in range(n):
            if t2 > 0:
                if t2 not in a:
                    break
                t2 -= 1
            if t1 <= n:
                if t1 not in a:
                    break
                t1 += 1
            ans += 1
            
        
            
        print(ans)


main()

t = int(input())
for _ in range(t):
    n, s, k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a = set(a)
    for i in range(0, 1005):
        if 1 <= s+i <= n:
            if s+i not in a:
                print(i)
                break
        if 1 <= s-i <= n:
            if s-i not in a:
                print(i)
                break
                 


for _ in range(int(input())):
    n,s,k = map(int,input().split())
    l = [*map(int,input().split())]
    dic = set(l)
    cnt = 0
    flag1 = False
    for i in range(s,n+1):
        if i not in dic:
            flag1 = True
            break
        else:
            cnt += 1
    cnt1 = 0
    flag = False
    for i in range(s,0,-1):
        if i not in dic:
            flag = True
            break
        else:
            cnt1 += 1
    if(flag and flag1):
        print(min(cnt,cnt1))
    elif(flag1):
        print(cnt)
    else:
        print(cnt1)

def solve(n,s,closed):
    min_moves = float('inf')
    floor = s
    time = 0
    while floor > 0:
        if floor not in closed:
            min_moves = min(min_moves,time)
            break
        floor -= 1
        time += 1

    floor = s
    time = 0
    while floor <= n:
        if floor not in closed:
            min_moves = min(min_moves,time)
            break
        floor += 1
        time += 1

    print(min_moves)

def main():
    t = int(input())
    for i in range(t):
        n,s,k = list(map(int,input().split()))
        closed = set(map(int,input().split()))
        solve(n,s,closed)

    
main()


for _ in range(int(input())):
    n, s, k = list(map(int,input().split()))
    a = set(map(int, input().split()))
    if s not in a:
        print(0)
        continue
    t1, t2 = 1, 1
    i = s
    ans=[]
    while i-t1 >= 1:
        if i-t1 in a:
            t1 += 1
        else:
            ans.append(t1)
            break
    while i+t2 <= n:
        if i + t2 in a:
            t2 += 1
        else:
            ans.append(t2)
            break
    print(min(ans))