#include "ricehub.h"
#include "bits/stdc++.h"
using namespace std;
long long sl, sh;
multiset<int> lo, hi;
void balance () {
if (lo.size() > hi.size() + 1) {
int top = *lo.rbegin();
lo.erase(lo.find(top));
hi.insert(top);
sl -= top;
sh += top;
} else if (hi.size() > lo.size()) {
int top = *hi.begin();
hi.erase(hi.find(top));
lo.insert(top);
sh -= top;
sl += top;
}
}
void ins(int x) {
if (lo.empty() || x <= *lo.rbegin()) {
sl += x;
lo.insert(x);
} else {
sh += x;
hi.insert(x);
}
balance();
}
void rem (int x) {
if (x <= *lo.rbegin()) {
lo.erase(lo.find(x));
sl -= x;
} else {
hi.erase(hi.find(x));
sh -= x;
}
balance();
}
long long getDis() {
int med = *lo.rbegin();
long long ret = 0;
ret += (long long) med * lo.size() - sl;
ret += sh - (long long) med * hi.size();
return ret;
}
void debug () {
cout << "LO: ";
for (int u : lo) cout << u << ' ';
cout << '\n';
cout << "HI: ";
for (int u : hi) cout << u << ' ';
cout << '\n';
cout << "Med, Dis: " << *lo.rbegin() << ' ' << getDis() << '\n';
}
int besthub(int R, int L, int X[], long long B) {
int l = 1, r = R;
while (l < r) {
bool ok = false;
int mid = (l + r + 1) >> 1;
for (int i = 0; i < mid; ++i) {
ins(X[i]);
}
if (getDis() <= B) {
ok = true;
}
//debug();
for (int i = mid; i < R; ++i) {
rem(X[i - mid]);
ins(X[i]);
if (getDis() <= B) {
ok = true;
}
//debug();
}
if (ok) {
l = mid;
} else {
r = mid - 1;
}
sl = 0, sh = 0;
lo.clear();
hi.clear();
cout << '\n';
}
return l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
304 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 |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 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 |
308 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 |
304 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
2 ms |
340 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 |
312 KB |
Output is correct |
12 |
Correct |
2 ms |
316 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
9 ms |
340 KB |
Output is correct |
22 |
Correct |
9 ms |
468 KB |
Output is correct |
23 |
Correct |
8 ms |
596 KB |
Output is correct |
24 |
Correct |
9 ms |
576 KB |
Output is correct |
25 |
Correct |
8 ms |
572 KB |
Output is correct |
26 |
Correct |
8 ms |
596 KB |
Output is correct |
27 |
Correct |
8 ms |
548 KB |
Output is correct |
28 |
Correct |
8 ms |
548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
980 KB |
Output is correct |
2 |
Correct |
39 ms |
980 KB |
Output is correct |
3 |
Correct |
337 ms |
6416 KB |
Output is correct |
4 |
Correct |
314 ms |
6444 KB |
Output is correct |
5 |
Correct |
114 ms |
2004 KB |
Output is correct |
6 |
Correct |
114 ms |
2004 KB |
Output is correct |
7 |
Correct |
305 ms |
6164 KB |
Output is correct |
8 |
Correct |
303 ms |
6164 KB |
Output is correct |
9 |
Correct |
147 ms |
1956 KB |
Output is correct |
10 |
Correct |
137 ms |
1956 KB |
Output is correct |
11 |
Correct |
307 ms |
6432 KB |
Output is correct |
12 |
Correct |
318 ms |
6496 KB |
Output is correct |
13 |
Correct |
112 ms |
2004 KB |
Output is correct |
14 |
Correct |
117 ms |
2048 KB |
Output is correct |
15 |
Correct |
249 ms |
4812 KB |
Output is correct |
16 |
Correct |
226 ms |
4864 KB |
Output is correct |
17 |
Correct |
264 ms |
5772 KB |
Output is correct |
18 |
Correct |
273 ms |
5756 KB |
Output is correct |
19 |
Correct |
292 ms |
6060 KB |
Output is correct |
20 |
Correct |
306 ms |
6116 KB |
Output is correct |