#include "ricehub.h"
#include<bits/stdc++.h>
using namespace std;
int besthub(int n, int L, int x[], long long int B) {
long long int ans = 0, h = x[0], s = 0, l = 0, r = 1;
deque<long long int> left, right;
for (long long int i = 1; i < n; i++) {
if (s+abs(x[i]-x[0]) <= B) {
s += abs(x[i]-x[0]);
right.push_back(x[i]);
r = i;
}
}
ans = right.size()+1;
for (long long int i = 1; i < n; i++) {
long long int diff = abs(x[i]-h);
left.push_back(h);
s += left.size()*diff;
s -= right.size()*diff;
if (!right.empty()) {
right.pop_front();
}
h = x[i];
while(!left.empty() && s > B) {
s -= abs(left.front()-h);
left.pop_front();
l++;
}
if (r < i || right.empty()) {
r = i;
}
while(!left.empty() && r+1 < n && abs(h-left.front()) >= abs(h-x[r+1])) {
s = s-abs(h-left.front())+abs(h-x[r+1]);
left.pop_front();
right.push_back(x[r+1]);
l++;
r++;
}
while(r+1 < n && s+abs(h-x[r+1]) <= B) {
s += abs(h-x[r+1]);
right.push_back(x[r+1]);
r++;
}
if (left.empty()) {
l = i;
}
while(l-1 >= 0 && s+abs(h-x[l-1]) <= B) {
s += abs(h-x[l-1]);
left.push_back(x[l-1]);
l--;
}
ans = max(ans, (long long int) left.size() + (long long int) right.size() + 1);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
0 ms |
348 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 |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 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 |
1 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 |
0 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
19 ms |
3424 KB |
Output is correct |
4 |
Correct |
10 ms |
3420 KB |
Output is correct |
5 |
Correct |
6 ms |
2760 KB |
Output is correct |
6 |
Correct |
5 ms |
2908 KB |
Output is correct |
7 |
Correct |
10 ms |
4184 KB |
Output is correct |
8 |
Correct |
10 ms |
4188 KB |
Output is correct |
9 |
Correct |
5 ms |
2908 KB |
Output is correct |
10 |
Correct |
4 ms |
2876 KB |
Output is correct |
11 |
Correct |
10 ms |
4444 KB |
Output is correct |
12 |
Correct |
14 ms |
4500 KB |
Output is correct |
13 |
Correct |
5 ms |
2908 KB |
Output is correct |
14 |
Correct |
5 ms |
2908 KB |
Output is correct |
15 |
Correct |
8 ms |
3932 KB |
Output is correct |
16 |
Correct |
9 ms |
3940 KB |
Output is correct |
17 |
Correct |
10 ms |
4188 KB |
Output is correct |
18 |
Correct |
11 ms |
4312 KB |
Output is correct |
19 |
Correct |
12 ms |
4440 KB |
Output is correct |
20 |
Correct |
11 ms |
4296 KB |
Output is correct |