-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreplace_for_x.cpp
52 lines (46 loc) · 1.21 KB
/
replace_for_x.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
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int test;
cin >> test;
while (test--){
int n, x, p, k, ans=0;
cin >> n >> x >> p >> k;
vector<int> a(n+5);
for (int i=1; i<=n; i++)
cin >> a[i];
sort(a.begin()+1, a.begin()+1+n);
if (p<k){
if (x>a[p]) {
cout << -1 << "\n";
return;
}
while (x<a[p] && p>=1) ans++, p--;
cout << ans << "\n";
}
else if (p>k){
if (x<a[p]) {
cout << -1 << "\n";
return;
}
while (x>a[p] && p<=n)
ans++, p++;
cout << ans << "\n";
}
else{
if (a[p]==x) cout << 0 <<"\n";
else if (a[p]<x){
while (x>a[p] && p<=n)
ans++, p++;
cout << ans << "\n";
}
else {
while (x<a[p] && p>=1)
ans++, p--;
cout << ans << "\n";
}
}
}
return 0;
}