#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
int besthub(int R, int L, int x[], long long B)
{
vector<long long>X(R+1);
X[0] = -(long long)1e18;
for (int i = 1; i <= R; i++){
X[i] = x[i - 1];
}
int l = 1, r = R;
long long pf[R+1];
pf[0] = 0;
for (int i = 1; i<=R; i++){
pf[i] = pf[i - 1] + X[i];
}
function<bool(int)>check = [&](int mid){
int r = 0;
int l = 1;
while(r + 1 <= R){
++r;
while(r - l + 1 > mid){
++l;
}
long long avg = (pf[r] - pf[l - 1]) / mid;
if (r - l + 1 == mid){
int itt = upper_bound(X.begin(), X.end(), avg) - X.begin();
for (auto it : {itt,itt-1,itt+1}){
if (it < l || it > r)continue;
long long sum = 0;
if (it != l){
sum += (it - l) * X[it] - (pf[it-1] - pf[l-1]);
}
if (it != r){
sum += -(r - it) * X[it] + (pf[r] - pf[it]);
}
if (sum <= B)return true;
}
}
}
return false;
};
while(l<=r){
int mid = (l+r) / 2;
if (check(mid)){
l = mid + 1;
}else{
r = mid - 1;
}
}
return l - 1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
312 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
324 KB |
Output is correct |
22 |
Correct |
2 ms |
328 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
596 KB |
Output is correct |
2 |
Correct |
12 ms |
724 KB |
Output is correct |
3 |
Correct |
11 ms |
3284 KB |
Output is correct |
4 |
Correct |
11 ms |
3284 KB |
Output is correct |
5 |
Correct |
20 ms |
1620 KB |
Output is correct |
6 |
Correct |
17 ms |
1620 KB |
Output is correct |
7 |
Correct |
10 ms |
3008 KB |
Output is correct |
8 |
Correct |
10 ms |
3028 KB |
Output is correct |
9 |
Correct |
21 ms |
1492 KB |
Output is correct |
10 |
Correct |
23 ms |
1492 KB |
Output is correct |
11 |
Correct |
11 ms |
3300 KB |
Output is correct |
12 |
Correct |
11 ms |
3264 KB |
Output is correct |
13 |
Correct |
31 ms |
1620 KB |
Output is correct |
14 |
Correct |
29 ms |
1620 KB |
Output is correct |
15 |
Correct |
11 ms |
2492 KB |
Output is correct |
16 |
Incorrect |
12 ms |
2472 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |