ConneR and the A.R.C. Markland-N

Description
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.

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
题目大意
ConneR在n层大楼的的第s层中,每层楼都有餐厅,ConneR要去餐厅,但被告知大楼中有k层的餐厅关门了,现已知关闭的餐厅的楼层有哪些,问ConneR最少走几层就可以用餐?
思路
这个题目注意到数据范围10^9,又看了看题目,k最大不会超过1000的,那么只有s上下1000的数据是有用的,只需要记录这些就可以了,再上下遍历查找最近的一个就可以。

#include <stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,s,k;
        int up[1001]={0},down[1001]={0};
        int i,j;
        scanf("%d%d%d",&n,&s,&k);
        for(i=0;i<k;i++)
        {
            int x;
            scanf("%d",&x);
            if(s==x)
            {
                down[0]=1;
                up[0]=1;
            }
            else if(s-x<1000 && s-x>0)
            {
                down[s-x]=1;
            }

            else if(x-s<1000 && x-s>0)
            {
                up[x-s]=1;
            }
        }
        for(i=0;i<=1000;i++)
        {
            if(!down[i] && s-i>0)//注意楼层数的限制
            {
                printf("%d\n",i);
                break;
            }
            if(!up[i] && s+i<=n)
            {
                printf("%d\n",i);
                break;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值