A.R.C. Markland-N is a tall building with nn floors numbered from 11 to nn. 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 ss of the building. On each floor (including floor ss, of course), there is a restaurant offering meals. However, due to renovations being in progress, kk 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 tt (1≤t≤10001≤t≤1000) — the number of test cases in the test. Then the descriptions of tt test cases follow.
The first line of a test case contains three integers nn, ss and kk (2≤n≤1092≤n≤109, 1≤s≤n1≤s≤n, 1≤k≤min(n−1,1000)1≤k≤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 kk distinct integers a1,a2,…,aka1,a2,…,ak (1≤ai≤n1≤ai≤n) — the floor numbers of the currently closed restaurants.
It is guaranteed that the sum of kk over all test cases does not exceed 10001000.
Output
For each test case print a single integer — the minimum number of staircases required for ConneR to walk from the floor ss to a floor with an open restaurant.
ExampleInput
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
题意
一栋楼有n个楼层,每层一家饭店,你在s层,有k家饭店关门,求去最近的饭店最少爬几层楼
代码
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int t,n,s,k;
int main()
{
cin>>t;
while(t--)
{
map<int,int>mp;
scanf("%d%d%d",&n,&s,&k);
int x;
for(int i=1;i<=k;i++)
{
scanf("%d",&x);
mp[x]=1;
}
for(int i=0;i<=n;i++)
if((!mp[s-i]&&s-i>=1)||(!mp[s+i]&&s+i<=n)) //下楼或上楼
{
printf("%d\n",i);
break;
}
}
return 0;
}