其他分享
首页 > 其他分享> > ConneR and the A.R.C. Markland-N (CF-1293A)

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;
}

 

Alex_McAvoy 发布了1871 篇原创文章 · 获赞 702 · 访问量 194万+ 他的留言板 关注

标签:return,res,LL,test,1293A,ConneR,Markland,include,mod
来源: https://blog.csdn.net/u011815404/article/details/104056395