//#include "ricehub.h"
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
int n, l;
const int dydis = 1e5 + 10;
const long long inf = 1e18;
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 besthub(int N, int LL, int mas[], long long B) {
n = N; l = LL;
for(int i = 0; i < n; i++) pref[i] = 1ll * mas[i] + (i == 0 ? 0ll : pref[i-1]);
int ans = 0;
int L = 0; int R = 0;
long long cur = 0;
for(int i = 0; i < n; i++){
if(mas[i] - mas[0] + cur > B) break;
R = i;
cur += abs(mas[0] - mas[i]);
}
ans = R - L + 1;
for(int i = 1; i < n; i++){
if(i > R) R = i;
long long SR = sm(i, R) - 1ll * (R - i + 1) * mas[i];
long long SL = 1ll * (i - L+ 1) * mas[i] - sm(L, i);
long long cur = SR + SL;
// cout << "i = " << i << ", L = " << L << ", R = " << R << "cur = " << cur << endl;
while(cur > B){
long long jeiK = abs(mas[L] - mas[i]);
long long jeiD = abs(mas[R] - mas[i]);
// cout << "L, R = " << L << "; " << R << ", cur = " << cur << endl;
if(jeiK > jeiD){
cur -= jeiK;
L++;
}else{
cur -= jeiD;
R--;
}
}
// cout << "po pirmo, L = " << L <<", R = " << R << ", cur = " << cur << endl;
while(cur <= B){
long long jeiK = (L == 0 ? inf : abs(mas[L-1] - mas[i]));
long long jeiD = (R == n-1 ? inf : abs(mas[R+1] - mas[i]));
if(min(jeiK, jeiD) + cur > B) break;
if(jeiK < jeiD){
L--;
cur += jeiK;
}else{
R++;
cur += jeiD;
}
}
// cout << "i = " << i << ", L = " << L << ", R = " << R << "cur = " << cur << endl<<endl;
ans = max(ans, R - L + 1);
}
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 |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 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 |
208 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 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 |
208 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
460 KB |
Output is correct |
3 |
Correct |
15 ms |
1500 KB |
Output is correct |
4 |
Correct |
16 ms |
2508 KB |
Output is correct |
5 |
Correct |
7 ms |
1228 KB |
Output is correct |
6 |
Correct |
7 ms |
1228 KB |
Output is correct |
7 |
Correct |
13 ms |
2252 KB |
Output is correct |
8 |
Correct |
14 ms |
2212 KB |
Output is correct |
9 |
Correct |
7 ms |
1100 KB |
Output is correct |
10 |
Correct |
7 ms |
1100 KB |
Output is correct |
11 |
Correct |
17 ms |
2452 KB |
Output is correct |
12 |
Correct |
16 ms |
2508 KB |
Output is correct |
13 |
Incorrect |
8 ms |
1228 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |