#include "ricehub.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
int besthub(int n, int L, int X[], long long b)
{
vector <ll> fields;
for (int i = 0; i < n; i++) fields.emplace_back(X[i]);
if (n <= 5e3)
{
ll cur = 0;
if (n == 1) return 1;
int output = 0;
ll p;
int left, right;
for (int i = 0; i < n; i++)
{
p = fields[i];
cur = b;
left = i - 1, right = i + 1;
while (true)
{
if (left < 0 && right == n)
{
break;
} else if (left < 0)
{
if (cur >= fields[right] - p) {
cur -= fields[right] - p;
right++;
} else break;
} else if (right == n)
{
if (cur >= p - fields[left]) {
cur -= p - fields[left];
left--;
} else break;
}
else if (p - fields[left] < fields[right] - p)
{
if (cur >= p - fields[left]) {
cur -= p - fields[left];
left--;
} else break;
} else {
if (cur >= fields[right] - p) {
cur -= fields[right] - p;
right++;
} else break;
}
}
output = max(output, right - left - 1);
}
return output;
}
auto pos = fields.begin(), lt = fields.begin(), rt = fields.begin() + 1;
ll cur = 0;
if (n == 1) return 1;
long output = 0;
while (rt != fields.end())
{
while (cur > b)
{
cur -= *pos - *lt;
lt++;
}
while (rt != fields.end() && cur + (*rt - *pos) <= b)
{
cur += (*rt - *pos);
rt++;
}
while (rt != fields.end() && (*pos - *lt) > (*rt - *pos))
{
cur += (*rt - *pos) - (*pos - *lt);
lt++, rt++;
while (rt != fields.end() && cur + (*rt - *pos) <= b)
{
cur += (*rt - *pos);
rt++;
}
}
output = max(output, ((rt - fields.begin()) - (lt - fields.begin())));
pos++;
if (rt != fields.end())
{
cur += (*pos - *(pos - 1)) * ((pos - fields.begin()) - (lt - fields.begin()));
cur += (*(pos - 1) - *pos) * ((rt - fields.begin()) - (pos - fields.begin()));
}
}
// cerr << output << "\n";
return output;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
0 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 |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
308 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 |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
308 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 |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 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 |
312 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 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 |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
212 KB |
Output is correct |
14 |
Correct |
2 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 |
3 ms |
212 KB |
Output is correct |
18 |
Correct |
4 ms |
212 KB |
Output is correct |
19 |
Correct |
5 ms |
212 KB |
Output is correct |
20 |
Correct |
5 ms |
212 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
444 KB |
Output is correct |
23 |
Correct |
36 ms |
468 KB |
Output is correct |
24 |
Correct |
34 ms |
452 KB |
Output is correct |
25 |
Correct |
94 ms |
460 KB |
Output is correct |
26 |
Correct |
82 ms |
468 KB |
Output is correct |
27 |
Correct |
20 ms |
468 KB |
Output is correct |
28 |
Correct |
19 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
852 KB |
Output is correct |
2 |
Correct |
3 ms |
892 KB |
Output is correct |
3 |
Correct |
15 ms |
2832 KB |
Output is correct |
4 |
Correct |
13 ms |
2856 KB |
Output is correct |
5 |
Correct |
6 ms |
1472 KB |
Output is correct |
6 |
Correct |
6 ms |
1464 KB |
Output is correct |
7 |
Correct |
11 ms |
2652 KB |
Output is correct |
8 |
Correct |
13 ms |
2576 KB |
Output is correct |
9 |
Correct |
6 ms |
1428 KB |
Output is correct |
10 |
Correct |
6 ms |
1428 KB |
Output is correct |
11 |
Correct |
13 ms |
2876 KB |
Output is correct |
12 |
Correct |
15 ms |
2884 KB |
Output is correct |
13 |
Correct |
7 ms |
1488 KB |
Output is correct |
14 |
Correct |
7 ms |
1472 KB |
Output is correct |
15 |
Correct |
13 ms |
2520 KB |
Output is correct |
16 |
Correct |
11 ms |
2508 KB |
Output is correct |
17 |
Correct |
12 ms |
2720 KB |
Output is correct |
18 |
Correct |
18 ms |
2668 KB |
Output is correct |
19 |
Correct |
13 ms |
2756 KB |
Output is correct |
20 |
Correct |
13 ms |
2748 KB |
Output is correct |