#include<bits/stdc++.h>
using namespace std;
vector<int> a;
int n;
int binser(int x) {
int l=0, r=n-1, ans;
while(l<=r) {
int mid=l+r>>1;
if(a[mid]<x) ans=mid, l=mid+1;
else r=mid-1;
}
return ans;
}
bool can(int w, int p, int q) {
int idx=0;
while(idx<n) {
if(p>1 && q) {
int BIG=binser(a[idx]+2*w);
if(BIG==n-1) return 1;
int SMALL=binser(a[idx]+w);
if(SMALL==n-1) return 1;
SMALL=binser(a[SMALL+1]+w);
if(SMALL==n-1) return 1;
if(SMALL>BIG) p-=2, idx=SMALL;
else q--, idx=BIG;
} else if(p && q) {
int BIG=binser(a[idx]+2*w), SMALL=binser(a[idx]+w);
if(BIG==n-1 || SMALL==n-1) return 1;
if(BIG==SMALL) p--, idx=SMALL;
else q--, idx=BIG;
} else if(p) idx=binser(a[idx]+w), p--;
else if(q) idx=binser(a[idx]+2*w), q--;
else return 0;
idx++;
}
return 1;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int p, q; cin >> n >> p >> q;
a.resize(n);
for(int &p : a) cin >> p;
sort(a.begin(), a.end());
int l=1, r=1e9, w=1e9;
while(l<=r) {
int mid=l+r>>1;
if(can(mid, p, q)) w=mid, r=mid-1;
else l=mid+1;
}
cout << w << '\n';
return 0;
}
Compilation message
watching.cpp: In function 'int binser(int)':
watching.cpp:8:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
8 | int mid=l+r>>1;
| ~^~
watching.cpp: In function 'int main()':
watching.cpp:46:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid=l+r>>1;
| ~^~
watching.cpp: In function 'int binser(int)':
watching.cpp:12:9: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
12 | return ans;
| ^~~
watching.cpp: In function 'bool can(int, int, int)':
watching.cpp:34:6: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
34 | idx++;
| ~~~^~
watching.cpp:29:4: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
29 | if(BIG==SMALL) p--, idx=SMALL;
| ^~
watching.cpp:29:4: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
watching.cpp:24:4: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
24 | if(SMALL>BIG) p-=2, idx=SMALL;
| ^~
watching.cpp:22:24: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
22 | SMALL=binser(a[SMALL+1]+w);
| ~~~~~^~
watching.cpp:24:4: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
24 | if(SMALL>BIG) p-=2, idx=SMALL;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
336 KB |
Output is correct |
4 |
Correct |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
328 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |