[Codeforces] Codeforces Round #614 (Div. 2) A. ConneR and the A.R.C. Markland-N

2020年12月15日

問題

方針

\( n \) 階建てビルの \( s \) 階から一番近いレストランを探します。フロア \( a_i \) のレストランは閉店しているので、\( s – k \) から \( s + k \) の範囲を全探索を行い、最小値を求めます。

コード

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    int t;
    cin >> t;
    int a[1000]{};
    for (int i = 0; i < t; i++) {
        int n, s, k;
        cin >> n >> s >> k;
        set<int> b;
        for (int j = 0; j < k; j++) {
            cin >> a[j];
            b.insert(a[j]);
        }
        int ans = n;
        for (int i = max(1, s - k); i <= min(n, s + k + 1); i++) {
            if (b.count(i) == 0) {
                ans = min(ans, abs(s - i));
            }
        }
        cout << ans << "\n";
    }
    return 0;
}