#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
int besthub(int R, int L, int X[], long long B)
{
long long pre[R];
pre[0] = X[0];
for(int i = 1;i < R;i++){
pre[i] = pre[i-1] + X[i];
}
long long ans = 0;
for(int i = 0;i <= R;i++){
int pos = X[i];
int lr = lower_bound(X,X+R,pos) - X;
int rl = upper_bound(X,X+R,pos) - X;
int low = 0;
int high = min(lr + 2, R - rl + 2);
//cout << pos << " " << lr << " " << rl << " ";
while(true){
if(low == high - 1) break;
int s = (low + high) / 2;
int ll = lr - s;
int rr = rl + s;
if(ll < 0 || rr > R){
high = s;
continue;
}
long long sum = 0ll;
if(lr > 0) sum += pre[lr-1];
if(ll > 0) sum -= pre[ll-1];
sum = s * pos - sum;
if(rr > 0) sum += pre[rr-1];
if(rl > 0) sum -= pre[rl-1];
sum -= s * pos;
//cout << "\n" << pos << " " << sum << " " << s << "wow\n";
if(sum > B){
high = s;
}
else{
low = s;
}
}
int ll = lr - low;
int rr = rl + low;
long long sum = 0ll;
if(lr > 0) sum += pre[lr-1];
if(ll > 0) sum -= pre[ll-1];
sum = low * pos - sum;
if(rr > 0) sum += pre[rr-1];
if(rl > 0) sum -= pre[rl-1];
//assert(lr-1 >= 0);
//assert(rr-1 >= 0);
//assert(rl-1 >= 0);
sum -= low * pos;
long long nos = rr - ll;
ans = max(nos, ans);
if(ll > 0 && sum + (long long)(abs(pos - X[ll-1])) <= B){
ans = max(nos+1,ans);
}
if(rr < R && sum + (long long)(abs(pos - X[rr])) <= B){
ans = max(nos+1,ans);
}
//cout << low << "\n";
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
356 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
256 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
2 ms |
256 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
256 KB |
Output is correct |
12 |
Correct |
2 ms |
256 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
2 ms |
256 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
3 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
640 KB |
Output is correct |
3 |
Correct |
32 ms |
1508 KB |
Output is correct |
4 |
Correct |
30 ms |
1536 KB |
Output is correct |
5 |
Correct |
12 ms |
896 KB |
Output is correct |
6 |
Correct |
12 ms |
896 KB |
Output is correct |
7 |
Incorrect |
23 ms |
1536 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |