//#include "ricehub.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int besthub(int R, int L, int positions[], long long B){
int n = R;
int left = 0;
int max_len = 0;
vector<ll> prefix_vec(n,0);
prefix_vec[0] = positions[0];
//calculating prefix sum
for(int i = 1; i < n; i++){
prefix_vec[i] = prefix_vec[i-1] + positions[i];
}
//sliding window concepts
for(int right = 0; right < n; right++){
int median = left + (right-left)/2;
ll left_sum = (median > 0) ? prefix_vec[median - 1] - ((left > 0)?prefix_vec[left-1]:0):0;
ll left_cost = (ll)(median-left)*positions[median] - left_sum;
ll right_cost = (prefix_vec[right] - prefix_vec[median]) - (ll)(right - median)*positions[median];
ll currentCost = left_cost + right_cost;
while(currentCost > B && median >= left){
left++;
median = left + (right-left)/2;
left_sum = (median > 0) ? prefix_vec[median - 1] - ((left > 0)?prefix_vec[left-1]:0):0;
left_cost = (ll)(median-left)*positions[median] - left_sum;
right_cost = (prefix_vec[right] - prefix_vec[median]) - (ll)(right - median)*positions[median];
currentCost = left_cost + right_cost;
}
max_len = max(max_len,right-left+1);
}
return max_len;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
396 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
9 ms |
3420 KB |
Output is correct |
4 |
Correct |
8 ms |
3420 KB |
Output is correct |
5 |
Correct |
5 ms |
2908 KB |
Output is correct |
6 |
Correct |
4 ms |
2908 KB |
Output is correct |
7 |
Correct |
7 ms |
3420 KB |
Output is correct |
8 |
Correct |
7 ms |
3416 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
4 ms |
2892 KB |
Output is correct |
11 |
Correct |
7 ms |
3420 KB |
Output is correct |
12 |
Correct |
7 ms |
3420 KB |
Output is correct |
13 |
Correct |
3 ms |
2908 KB |
Output is correct |
14 |
Correct |
4 ms |
2908 KB |
Output is correct |
15 |
Correct |
5 ms |
3164 KB |
Output is correct |
16 |
Correct |
6 ms |
3164 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
8 ms |
4188 KB |
Output is correct |
19 |
Correct |
9 ms |
4304 KB |
Output is correct |
20 |
Correct |
8 ms |
3420 KB |
Output is correct |