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

14372 단어 codeforces
A. ConneR and the A.R.C. Markland-N
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Sakuzyo - Imprinting 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.
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.
제목:
n층 건물이 하나 있는데 k층이 있는 식당은 문을 닫았어요. 어떤 식당은 s층에 있는데 가장 가까운 식당은 몇 층으로 가야 하는지 물어봤어요.
아이디어:
데이터가 109까지 나와서 해시를 사용할 수 없기 때문에 닫힌 식당을 다음 순서대로 1~n까지 훑어볼 수 있다. 만약에 이게 닫힌 것이 아니라면 층간 거리를 계산한다. 그러나 최적화가 되지 않으면 T, 층s로 가까워지기 때문에 거리가 점점 가까워질 것이다. 가장 가까운 것이 나타날 때까지 점점 멀어질 것이다. 이럴 때 바로 브레이크를 하면 된다.
#include 
#include 
#include 
#include 
using namespace std;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
inline void out(int x) {
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
const int maxn = 1e+3 + 10;
const int maxm = 1e+9;
 int main() {
     int t, n, s, k;
     read(t);
     while (t--) {
        int a[maxn] = {0};
        read(n), read(s), read(k);
        for (int i = 0; i < k; i++) read(a[i]);
        sort(a, a + k);
        int x = 0, Min = 0x7fffffff;
        for (int i = 1; i <= n; i++) {
            if (a[x] == i) {
                while (a[x] == i) x++;
                continue;
            }
            Min = min(Min, abs(i - s));
            if (abs(i - s) > Min) break;
        }
        printf("%d
"
, Min); } return 0; }

좋은 웹페이지 즐겨찾기