#include "ricehub.h"
#include <bits/stdc++.h>
#define ub upper_bound
#define lb lower_bound
using namespace std;
int besthub(int R, int L, int X[], long long B)
{
// for every median, calculate maximum res di kiri dan di kanan
// calculate yg exactly maximum res berapa
// rlog^2r
int a[R + 1];
a[0] = 0;
for(int i = 1; i <= R; ++i)
a[i] = X[i - 1];
long long pref[R + 1];
pref[0] = 0;
for(int i = 1; i <= R; ++i)
pref[i] = pref[i - 1] + a[i];
int ans = 1;
for(int i = 1; i <= R; ++i) {
long long l = 1, r = L;
while(l <= r) {
int mid = (l + r) / 2;
// cek klo cost ambil yg < mid brp, yg tepat jarak mid ada berapa
// cek lower dlu
int idx1 = ub(a + 1, a + R + 1, a[i] - mid) - a, cntmid = ub(a + 1, a + R + 1, a[i] - mid) - lb(a + 1, a + R + 1, a[i] - mid);
int idx2 = lb(a + 1, a + R + 1, a[i] + mid) - a - 1;
cntmid += ub(a + 1, a + R + 1, a[i] + mid) - lb(a + 1, a + R + 1, a[i] + mid);
// cari prefsum dr idx1 dan idx2
long long cost = 1ll * (i - idx1 + 1) * a[i] - (pref[i] - (idx1 - 1 >= 0 ? pref[idx1 - 1] : 0));
//cout << cost << endl;
cost += (pref[idx2] - pref[i - 1]) - 1ll * a[i] * (idx2 - i + 1);
//cout << i << " " << idx1 << " " << idx2 << " " << cost << " " << cntmid << endl;
if(cost > B) {
r = mid - 1;
continue;
}
else {
ans = max((long long)ans, abs(idx2 - idx1) + 1 + min((B - cost) / mid, (long long)cntmid));
l = mid + 1;
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
308 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 |
316 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 |
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 |
312 KB |
Output is correct |
19 |
Correct |
2 ms |
312 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 |
2 ms |
212 KB |
Output is correct |
24 |
Correct |
2 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 |
2 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
308 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
2 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
212 KB |
Output is correct |
9 |
Correct |
3 ms |
312 KB |
Output is correct |
10 |
Correct |
2 ms |
212 KB |
Output is correct |
11 |
Correct |
3 ms |
320 KB |
Output is correct |
12 |
Correct |
3 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
212 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
212 KB |
Output is correct |
19 |
Correct |
3 ms |
212 KB |
Output is correct |
20 |
Correct |
3 ms |
212 KB |
Output is correct |
21 |
Correct |
9 ms |
404 KB |
Output is correct |
22 |
Correct |
7 ms |
324 KB |
Output is correct |
23 |
Correct |
8 ms |
340 KB |
Output is correct |
24 |
Correct |
12 ms |
340 KB |
Output is correct |
25 |
Correct |
13 ms |
420 KB |
Output is correct |
26 |
Correct |
15 ms |
420 KB |
Output is correct |
27 |
Correct |
20 ms |
448 KB |
Output is correct |
28 |
Correct |
19 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
608 KB |
Output is correct |
2 |
Correct |
89 ms |
728 KB |
Output is correct |
3 |
Correct |
356 ms |
2916 KB |
Output is correct |
4 |
Correct |
505 ms |
2932 KB |
Output is correct |
5 |
Correct |
144 ms |
1416 KB |
Output is correct |
6 |
Correct |
142 ms |
1412 KB |
Output is correct |
7 |
Correct |
221 ms |
2644 KB |
Output is correct |
8 |
Correct |
305 ms |
2516 KB |
Output is correct |
9 |
Correct |
176 ms |
1364 KB |
Output is correct |
10 |
Correct |
178 ms |
1364 KB |
Output is correct |
11 |
Correct |
354 ms |
2916 KB |
Output is correct |
12 |
Correct |
499 ms |
2916 KB |
Output is correct |
13 |
Correct |
307 ms |
1468 KB |
Output is correct |
14 |
Correct |
291 ms |
1468 KB |
Output is correct |
15 |
Correct |
264 ms |
2240 KB |
Output is correct |
16 |
Correct |
377 ms |
2240 KB |
Output is correct |
17 |
Correct |
310 ms |
2628 KB |
Output is correct |
18 |
Correct |
469 ms |
2624 KB |
Output is correct |
19 |
Correct |
334 ms |
2776 KB |
Output is correct |
20 |
Correct |
503 ms |
2780 KB |
Output is correct |