#include "bits/stdc++.h"
#include "ricehub.h"
#define i64 long long
using namespace std;
const int sz = 2e5 + 10;
int a[sz];
int n;
i64 b;
bool che(int x) {
int ra = x / 2;
int ps = ra;
i64 sm = 0;
for (int i = 0; i < x; i ++) {
sm += abs(a[i] - a[ps]);
}
if (sm <= b) {
return true;
}
ps ++;
for (; ps < n - ra; ps ++) {
int pps = ps - 1; // previous ps
sm -= abs(a[pps - ra] - a[pps]); // delete leftmost
sm -= abs(a[pps] - a[ps]); // delete new ps
sm -= (ra - 1); // distance to right decreases
sm += abs(a[pps] - a[ps]); // add previous ps
sm += (ra - 1); // distance to left increases
sm += abs(a[ps + ra] - a[ps]); // add rightmost
if (sm <= b) {
return true;
}
}
return false;
}
bool chee(int x) {
int lra = x / 2 - 1;
int rra = x / 2;
int ps = lra;
i64 sm = 0;
for (int i = 0; i < x; i ++) {
sm += abs(a[i] - a[ps]);
}
if (sm <= b) {
return true;
}
for (; ps < n - rra; ps ++) {
int pps = ps - 1;
sm -= abs(a[pps - lra] - a[pps]); // delete leftmost
sm -= abs(a[pps] - a[ps]); // delete new ps
sm -= (rra - 1); // distance to right decreases
sm += abs(a[pps] - a[ps]); // add previous ps
sm += (lra - 1); // distance to left increases
sm += abs(a[ps + rra] - a[ps]); // add rightmost
if (sm <= b) {
return true;
}
}
return false;
}
int besthub(int R, int L, int X[], long long B)
{
int le, ri;
le = 0, ri = R + 1;
for (int i = 0; i < R; i ++) {
a[i] = X[i];
}
b = B;
n = R;
while (1 < ri - le) {
int mi = le + (ri - le) / 2;
bool b = (mi % 2) ? che(mi) : chee(mi);
if (b) {
le = mi;
} else {
ri = mi;
}
}
return le;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
13 ms |
1832 KB |
Output is correct |
4 |
Correct |
9 ms |
2140 KB |
Output is correct |
5 |
Correct |
4 ms |
1004 KB |
Output is correct |
6 |
Incorrect |
5 ms |
956 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |