CodeForces 1293A——ConneR and the A.R.C. Markland-N【签到题】

题目传送门


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 ) t (1≤t≤1000) t(1t1000) — 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 n n, s s s and k ( 2 ≤ n ≤ 10 k (2≤n≤ 10 k(2n109 , 1 ≤ s ≤ n , 1 ≤ k ≤ m i n ( n − 1 , 1000 ) ) , 1≤s≤n, 1≤k≤min(n−1,1000)) ,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 k distinct integers a 1 , a 2 , … , a k ( 1 ≤ a i ≤ n ) a1,a2,…,ak (1≤ai≤n) a1,a2,,ak(1ain) — the floor numbers of the currently closed restaurants.

It is guaranteed that the sum of k k k over all test cases does not exceed 1000 1000 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.


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.


题意

  • 一栋楼有 n n n个楼层,每层一个饭店,你在第 s s s层,有 k k k家关门的饭店,求去最近的开门的饭店的最少层数。

题解

  • 暴力即可,注意数据范围, 2 ≤ n ≤ 10 2≤n≤ 10 2n109,不能直接开数组 O ( 1 ) O(1) O(1)查找
  • 记得开 l o n g long long l o n g long long 即可

AC-Code

#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll a[maxn];
ll num;
int main() {
	int T;
	cin >> T;
	while (T--) {
		num = 0;
		ll n, s, k;
		cin >> n >> s >> k;
		for (ll i = 0; i < k; ++i) {
			cin >> a[num++];
		}
		sort(a, a + num);
		bool f = true;
		ll x = -0x7fffffff7fffffff, y = 0x7fffffff7fffffff;
		for (ll i = s; i >= s - k && i > 0 && f; --i) {
			ll* xi = lower_bound(a, a + num, i);
			if (xi == a + num || (*xi != i)) {
				x = i;
				f = false;
			}
		}
		f = true;
		for (ll i = s + 1; i <= s + k && i <= n && f; ++i) {
			ll* xi = lower_bound(a, a + num, i);
			if (xi == a + num || (*xi != i)) {
				y = i;
				f = false;
			}
		}
		ll ans = min(s - x, y - s);
		cout << ans << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值