#include <bits/stdc++.h>
using namespace std;
int besthub(int R, int L, int X[] , long long B){
int best = 0;
vector<long long> v(R);
for(int i = 0 ; i < R ; i++){
v[i] = (long long) X[i];
}
vector<long long> pref(R);
for(int i = 0 ; i < R ; i++){
if(i == 0) pref[i] = X[i];
else pref[i] = X[i] + pref[i - 1];
}
for(int i = 1 ; i < R ; i++){
int ans = 0;
long long s = 0;
int l = i - 1 , r = i + 1;
while(l >= 0 || r < R){
long long f_dist = (l >= 0 ? X[i] - X[l] : (long long)1e18) , s_dist = (r < R ? X[r] - X[i] : (long long)1e18);
if(f_dist >= s_dist){
int id = upper_bound(v.begin(),v.end() , v[i] + f_dist) - v.begin();
long long sum = pref[id - 1] - pref[i];
s += sum - (id - i - 1) * X[i];
if(s <= B) ans += (id - i - 1);
r = id;
}
else{
int id = lower_bound(v.begin(),v.end() , v[i] - s_dist) - v.begin();
long long sum = pref[i] - (id > 0 ? pref[id - 1] : 0);
s += (i - id) * X[i] - sum;
if(s <= B) ans += (i - id);
l = id - 1;
}
if(s > B) break;
}
while(l >= 0 || r < R){
if((l < 0) || (r < R && X[r] - X[i] <= X[i] - X[l])){
s += X[r] - X[i];
r++;
}
else if((r >= R) || (l >= 0 && X[i] - X[l] <= X[r] - X[i])){
s += X[i] - X[l];
l--;
}
if(s > B) break;
ans++;
}
best = max(best , ans);
if(best == R - 1) break;
}
return best + 1;
}
/*int main(){
int arr[] = {1,2,10,12,14};
cout << besthub(5 , 20 , arr , 6) << '\n';
}*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
280 KB |
Output is correct |
2 |
Correct |
1 ms |
220 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
167 ms |
692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |