#include "ricehub.h"
#include <cmath>
using namespace std;
bool poss(int len, long long budget, int *x, int n) {
long long cost = 0;
int a = 0;
int b = len - 1;
int mid = (a + b) / 2;
long long left = 0;
long long right = 0;
for (int i = a; i <= b; i++) {
cost += abs(x[i] - x[mid]);
}
left = mid - a;
right = b - mid;
if (cost <= budget) return true;
while (b + 1 < n) {
cost -= x[mid] - x[a];
cost += left * (x[mid + 1] - x[mid]);
cost -= right * (x[mid + 1] - x[mid]);
mid++;
a++;
b++;
cost += x[b] - x[mid];
if (cost <= budget) return true;
}
return false;
}
int besthub(int n, int l, int x[], long long budget) {
int a = 1;
int b = n;
int res = -1;
while (a <= b) {
int mid = (a + b) / 2;
if (poss(mid, budget, x, n)) {
res = mid;
a = mid + 1;
} else {
b = mid - 1;
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
0 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
0 ms |
364 KB |
Output is correct |
24 |
Correct |
0 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
0 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
492 KB |
Output is correct |
3 |
Correct |
19 ms |
1788 KB |
Output is correct |
4 |
Correct |
17 ms |
1772 KB |
Output is correct |
5 |
Correct |
8 ms |
876 KB |
Output is correct |
6 |
Correct |
11 ms |
1024 KB |
Output is correct |
7 |
Correct |
15 ms |
1516 KB |
Output is correct |
8 |
Correct |
15 ms |
1516 KB |
Output is correct |
9 |
Correct |
7 ms |
748 KB |
Output is correct |
10 |
Correct |
8 ms |
876 KB |
Output is correct |
11 |
Correct |
17 ms |
1772 KB |
Output is correct |
12 |
Correct |
17 ms |
1772 KB |
Output is correct |
13 |
Correct |
11 ms |
876 KB |
Output is correct |
14 |
Correct |
9 ms |
876 KB |
Output is correct |
15 |
Correct |
13 ms |
1388 KB |
Output is correct |
16 |
Correct |
13 ms |
1388 KB |
Output is correct |
17 |
Correct |
15 ms |
1644 KB |
Output is correct |
18 |
Correct |
15 ms |
1644 KB |
Output is correct |
19 |
Correct |
16 ms |
1644 KB |
Output is correct |
20 |
Correct |
16 ms |
1644 KB |
Output is correct |