-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.cc
109 lines (106 loc) · 2.81 KB
/
a.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#if defined(LOCAL)
const double _max_double_error = 1e-9;
#include "testutils.h"
#define L(x...) (debug(x, #x))
#define I(x, ...) (x)
#define C(x...) CHECK(x)
#else
#define L(x, ...) (x)
#define I(x, ...) (x)
#define C(x, ...) ;
#endif
#include <math.h>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ii = pair<int, int>;
using lu = unsigned long long;
using l = long long;
using vs = vector<string>;
using vii = vector<ii>;
using vl = vector<l>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vlu = vector<lu>;
using ll = pair<l, l>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using mll = unordered_map<l, l>;
using sl = unordered_set<l>;
const l INF = numeric_limits<l>::max();
const double EPS = 1e-10;
const double PI = 3.14159265358979323846;
const l e0 = 1, e3 = 1000, e5 = 100000, e6 = 10 * e5, e7 = 10 * e6,
e8 = 10 * e7, e9 = 10 * e8, l0 = 0, l1 = 1, l2 = 2;
const char lf = '\n';
#define all(x) begin(x), end(x)
#define F(a, b, c) for (l a = l(b); a < l(c); a++)
#define B(a, b, c) for (l a = l(c) - 1; a >= l(b); a--)
void solve(istream &in, ostream &out);
void solve_brute(istream &, ostream &);
bool interactive_judge(istream &, istream &, ostream &);
bool generate_random(l, ostream &);
bool solution_checker(istream &, istream &, istream &, ostream &);
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
#if defined(LOCAL)
tst::test_init(argc, argv);
// _generate_random_test = generate_random;
// _solve_brute = solve_brute;
// _judge = interactive_judge;
// _custom_solution_checker = solution_checker;
tst::maybe_run_tests(cin, cout);
#else
solve(cin, cout);
#endif
}
const l MOD = e9 + 7; // end of template
void solve(istream &in, ostream &out) {
l tcc; in >> tcc;
F(tc, 1, tcc + 1) {
l n, s, k; in >> n >> s >> k;
vl v(k); F(i, 0, k) in >> v[i];
sort(all(v));
auto u = lower_bound(all(v), s);
l i = distance(v.begin(), u);
if (i >= k or *u != s) {
out << 0 << lf;
continue;
}
l j = i;
l z = INF;
while (j - 1 >= 0 and v[j - 1] == v[j] - 1) j--;
if (j >= 0 && v[j] != 1) z = min(z, s - v[j] + 1);
j = i;
while (j + 1 < k and v[j + 1] == v[j] + 1) j++;
if (j < k and v[j] != n) z = min(z, v[j] + 1 - s);
out << z << lf;
}
}