题目传送门
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
The first line of a test case contains three integers , and 9
The second line of a test case contains k distinct integers
It is guaranteed that the sum of over all test cases does not exceed .
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.
题意
- 一栋楼有个楼层,每层一个饭店,你在第层,有家关门的饭店,求去最近的开门的饭店的最少层数。
题解
- 暴力即可,注意数据范围,9,不能直接开数组查找
- 记得开
AC-Code
#include <bits/stdc++.h>using namespace std;
#define
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;
}