#include "ricehub.h"
#include<bits/stdc++.h>
using namespace std;
int besthub(int R, int L, int X[], long long B)
{
int ans = 0;
vector<int64_t>pref(R + 1,0),dpref(R + 1,0);
vector<int64_t>suff(R + 1,0),dsuff(R + 1,0);
for (int i = 0 ;i<R - 1;++i){
pref[i + 1] = pref[i] + (X[i + 1] - X[i]);
dpref[i + 2] = pref[i + 1] + dpref[i + 1];
}
for (int i = R - 2 ;i>=0;--i){
suff[i] = suff[i + 1] + (X[i + 1] - X[i]);
dsuff[i] = suff[i] + dsuff[i + 1];
}
auto dist = [&](int l,int r){
return pref[r] * (r - l) - dpref[r] + dpref[l];
};
auto dist2 = [&](int l,int r){
return suff[l] * (r - l) + dsuff[r + 1] - dsuff[l + 1];
};
auto distt = [&](int l,int r){
int mid = (l + r)>>1;
return dist(l,mid) + dist2(mid,r);
};
for (int i = 0;i<R;++i){
int l = 0;
int left = 0,right = i;
while(left<=right){
int mid = (left + right)>>1;
if (dist(mid,i)<=B){
l = mid;
right = mid - 1;
}
else left = mid + 1;
}
int counts = i - l + 1;
int j = i + 1;
ans = max(ans,counts);
while(j<R && l<=i){
if (distt(l,j)<=B){
counts++;
++j;
}
else if (l == i){
break;
}
else{
counts--;
++l;
}
ans = max(ans,counts);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 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 |
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 |
2 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 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 |
2 ms |
336 KB |
Output is correct |
14 |
Correct |
2 ms |
340 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 |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
6 ms |
468 KB |
Output is correct |
22 |
Correct |
6 ms |
468 KB |
Output is correct |
23 |
Correct |
34 ms |
468 KB |
Output is correct |
24 |
Correct |
33 ms |
468 KB |
Output is correct |
25 |
Correct |
35 ms |
468 KB |
Output is correct |
26 |
Correct |
32 ms |
468 KB |
Output is correct |
27 |
Correct |
37 ms |
468 KB |
Output is correct |
28 |
Correct |
36 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
980 KB |
Output is correct |
2 |
Correct |
11 ms |
980 KB |
Output is correct |
3 |
Execution timed out |
1090 ms |
3768 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |