#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
int n, l;
const int dydis = 1e5 + 10;
long long pref[dydis];
long long sm(int l, int r){
if(l > r) return 0;
return pref[r] - (l == 0 ? 0ll : pref[l-1]);
}
int kek(int x, int mas[], int n){
return upper_bound(mas, mas+n, x) - lower_bound(mas, mas+n, x);
}
pair<long long, long long> f(int dst, int ind, int mas[], long long B){
int l = 0; int r = ind; int mid;
int L = ind+1; int R = ind-1;
while(l <= r){
mid = (l + r) / 2;
if(abs(mas[mid]-mas[ind]) < dst){
L = min(L, mid);
r = mid-1;
}else{
l = mid+1;
}
}
l = ind; r = n-1;
while(l <= r){
mid = (l + r) / 2;
if(abs(mas[mid] - mas[ind]) < dst){
R = max(R, mid);
l = mid+1;
}else{
r = mid-1;
}
}
long long SL = 1ll * (ind - L + 1) * mas[ind] - sm(L, ind);
long long SR = sm(ind, R) - 1ll * (R - ind + 1) * mas[ind];
long long lieka = B - SL - SR;
int pl = 0;
if(lieka > 0){
int galiu = lieka / dst;
galiu = min(galiu, kek(mas[ind] + dst, mas, n) + kek(mas[ind] - dst, mas, n));
pl = galiu;
}
// cout << "SR = " << sm(ind, R) << " - " << 1ll * (R - ind + 1) * mas[ind] << ", MAS[" << ind << "] = " << mas[ind] << " ";
// cout << "dst = " << dst << ", o i = " << ind << ", tai L = " << L << ", R = " << R << ", tai ret = " << SL << " + " << SR << endl;
return make_pair(SL + SR, max(R - L + 1, 0)+pl);
}
int besthub(int N, int L, int X[], long long B) {
n = N; l = L;
for(int i = 0; i < n; i++) pref[i] = 1ll * X[i] + (i == 0 ? 0ll : pref[i-1]);
long long ans = 0;
for(int i = 0; i < n; i++){
long long st = 1;
int l = 0, r = 1e9, mid;
while(l <= r){
mid = (l + r) / 2;
auto ff = f(mid, i, X, B);
if(ff.first <= B){
st = max(st, ff.second);
l = mid+1;
}else{
r = mid-1;
}
}
// cout << "i = " << i << ", st = " << st << endl;
ans = max(ans, st);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
208 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
204 KB |
Output is correct |
23 |
Correct |
2 ms |
208 KB |
Output is correct |
24 |
Correct |
2 ms |
300 KB |
Output is correct |
25 |
Correct |
2 ms |
300 KB |
Output is correct |
26 |
Correct |
2 ms |
204 KB |
Output is correct |
27 |
Correct |
2 ms |
204 KB |
Output is correct |
28 |
Correct |
3 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
3 ms |
304 KB |
Output is correct |
4 |
Correct |
3 ms |
308 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
3 ms |
332 KB |
Output is correct |
8 |
Correct |
3 ms |
308 KB |
Output is correct |
9 |
Correct |
2 ms |
204 KB |
Output is correct |
10 |
Correct |
2 ms |
204 KB |
Output is correct |
11 |
Correct |
3 ms |
204 KB |
Output is correct |
12 |
Correct |
3 ms |
324 KB |
Output is correct |
13 |
Correct |
6 ms |
332 KB |
Output is correct |
14 |
Correct |
4 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
204 KB |
Output is correct |
16 |
Correct |
2 ms |
204 KB |
Output is correct |
17 |
Correct |
4 ms |
204 KB |
Output is correct |
18 |
Correct |
3 ms |
204 KB |
Output is correct |
19 |
Correct |
4 ms |
204 KB |
Output is correct |
20 |
Correct |
3 ms |
332 KB |
Output is correct |
21 |
Correct |
14 ms |
332 KB |
Output is correct |
22 |
Correct |
14 ms |
332 KB |
Output is correct |
23 |
Correct |
19 ms |
316 KB |
Output is correct |
24 |
Correct |
16 ms |
400 KB |
Output is correct |
25 |
Correct |
19 ms |
332 KB |
Output is correct |
26 |
Correct |
18 ms |
408 KB |
Output is correct |
27 |
Correct |
21 ms |
404 KB |
Output is correct |
28 |
Correct |
22 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
660 KB |
Output is correct |
2 |
Correct |
67 ms |
588 KB |
Output is correct |
3 |
Correct |
524 ms |
2516 KB |
Output is correct |
4 |
Correct |
537 ms |
2520 KB |
Output is correct |
5 |
Correct |
197 ms |
1216 KB |
Output is correct |
6 |
Correct |
190 ms |
1228 KB |
Output is correct |
7 |
Correct |
512 ms |
2228 KB |
Output is correct |
8 |
Correct |
408 ms |
2228 KB |
Output is correct |
9 |
Correct |
219 ms |
1100 KB |
Output is correct |
10 |
Correct |
220 ms |
1176 KB |
Output is correct |
11 |
Correct |
530 ms |
2524 KB |
Output is correct |
12 |
Correct |
529 ms |
2608 KB |
Output is correct |
13 |
Correct |
226 ms |
1228 KB |
Output is correct |
14 |
Correct |
231 ms |
1268 KB |
Output is correct |
15 |
Correct |
386 ms |
1872 KB |
Output is correct |
16 |
Correct |
411 ms |
1956 KB |
Output is correct |
17 |
Correct |
464 ms |
2280 KB |
Output is correct |
18 |
Correct |
486 ms |
2280 KB |
Output is correct |
19 |
Correct |
507 ms |
2500 KB |
Output is correct |
20 |
Correct |
502 ms |
2500 KB |
Output is correct |