-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimal.cpp
62 lines (56 loc) · 1.55 KB
/
optimal.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout << #x << "->" << x << endl;
#define print(x) \
for (auto &x_ : x) cout << x_ << " "; \
cout << endl; \
s
int32_t main() {
int tc;
cin >> tc;
while (tc--) {
int n, x, p, k;
cin >> n >> x >> p >> k;
vector<int> a(n + 1, 0);
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a.begin(), a.end());
if (a[p] == x) {
cout << "0\n";
}
else if (p == k) {
if (x > a[p]) {
int index = p;
while (index <= n && a[index] < x) index++;
cout << (index - p) << endl;
} else if (x < a[p]) {
int index = p;
while (index >= 1 && a[index] > x) {
index--;
}
cout << (p - index) << endl;
}
}
else if (p < k) {
if (a[p] < x) {
cout << "-1\n";
} else {
int index = p;
while (index >= 1 && a[index] > x) {
index--;
}
cout << (p - index) << endl;
}
}
else if (p > k) {
if (a[p] > x) {
cout << "-1\n";
} else {
int index = p;
while (index <= n && a[index] < x) {
++index;
}
cout << (index - p) << endl;
}
}
}
}