ConneR and the A.R.C. Markland-N (CF-1293A)

Problem Description

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≤t≤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≤n≤109, 1≤s≤n, 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 k distinct integers a1,a2,…,ak (1≤ai≤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.

Examples

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

題意:t 組數據,每組有一棟 n 層高的每層都有餐廳的樓,現在有一個人在 s 樓要去吃飯,給出了 k 個關門的餐廳的層數,問在 s 樓的這個人,最少走幾層能成功吃到飯

思路:

k 的範圍是從 1 到 min(n-1,1000),也就是說最多有 1000 個關門的餐廳,最極限的情況下,從當前層 s 到 s+1000 均不合法

也就是說,只需要從 s 層開始向上下兩側枚舉,記錄下第一個合法的距離進行比較即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickMultPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL quickPowMod(LL a,LL b,LL mod){ LL res=1; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1; } return res; }
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-6;
const int MOD = 1000000000+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,1,-1,1,1};
const int dy[] = {1,-1,0,0,-1,1,-1,1};
using namespace std;

LL a[N];
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        LL n, s, k;
        scanf("%lld%lld%lld", &n, &s, &k);
        for (LL i = 1; i <= k; i++)
            scanf("%lld", &a[i]);

        LL limit = s - 1000;
        if (limit < 0)
            limit = 1;

        LL minn = INF;
        for (LL i = s; i >= limit; i--) {

            bool flag = true;
            for (LL j = 1; j <= k; j++) {
                if (a[j] == i) {
                    flag = false;
                    break;
                }
            }
            if (flag)
                minn = min(minn, s - i);
        }

        limit = s + 1000;
        if (limit > n)
            limit = n;

        for (LL i = s; i <= limit; i++) {
            bool flag = true;
            for (LL j = 1; j <= k; j++) {
                if (a[j] == i) {
                    flag = false;
                    break;
                }
            }
            if (flag)
                minn = min(minn, i - s);
        }

        printf("%lld\n", minn);
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章