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

本文介绍了一个算法问题——CodeForces-1293A,主角ConneR在一个拥有n层楼的大楼中,需要找到离他最近的开放餐厅。大楼中总共有k个餐厅关闭,ConneR从s楼层出发,算法需要计算出到达最近开放餐厅所需经过的最少楼梯数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

See the original article on https://dyingdown.github.io/2020/01/19/CodeForces-1293A-ConneR-and-the-A.R.C.-Markland-N/

A. ConneR and the A.R.C. Markland-N
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Sakuzyo - Imprinting

A.R.C. Markland-N is a tall building with n n n floors numbered from 1 to n n 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 s s of the building. On each floor (including floor s s s, of course), there is a restaurant offering meals. However, due to renovations being in progress, k k 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 ) t (1≤t≤1000) t(1t1000) — the number of test cases in the test. Then the descriptions of t t t test cases follow.

The first line of a test case contains three integers n , s n, s n,s and k ( 2 ≤ n ≤ 1 0 9 , 1 ≤ s ≤ n , 1 ≤ k ≤ m i n ( n − 1 , 1000 ) ) k (2≤n≤10^9, 1≤s≤n, 1≤k≤min(n−1,1000)) k(2n109,1sn,1kmin(n1,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 a 1 , a 2 , … , a k ( 1 ≤ a i ≤ n ) a_1,a_2,…,a_k (1≤a_i≤n) a1,a2,,ak(1ain) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of kk 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 s 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.

Analysis

Just check the floor from the initial floor to the bottom or to the top.

At first I use array to store the closed restaurant. However, I got Runtime Error. I don’t know why so I changed to map and it passed.

I use 0 to represents not closed and 1 for closed status.

Code

#include<bits/stdc++.h>
 
using namespace std;
 
int main() {
    long long t;
    cin >> t;
    while (t--) {
    	map<long long, int> arr;
        long long n, s, k;
        cin >> n >> s >> k;
        long long countup = 0, countdown = 0;
        for(long long i = 0; i < k; i ++) {
            long long a;
            cin >> a;
            arr[a] ++; // if it's closed, it's status is 1
        }
        arr[0] = 1;
        long long temp = s;
        while (arr[temp]) {
            countdown ++;
            temp ++;
            if(temp > n) {
                countdown = 20000000000;
                break;
            }
        }
        temp = s;
        while (arr[temp]) {
            countup ++;
            temp --;
            if(temp < 0) {
                countup = 20000000000;
                break;
            }
        }
//        cout << "Down: " << countdown << " Up: " << countup << endl;
        cout << min(countdown,countup) << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值