#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 1e5 + 5;
int n;
int a[N];
multiset <int> mtslo, mtshi;
ll sumlo, sumhi;
int median(){
return mtslo.empty() ? INT_MAX : *mtslo.rbegin();
}
ll cal_dist(){
int m = median();
return sumhi - (ll)m * isz(mtshi) + (ll)m * isz(mtslo) - sumlo;
}
void balance(){
while (isz(mtslo) > isz(mtshi) + 1){
int x = *mtslo.rbegin();
mtslo.erase(mtslo.find(x));
sumlo -= x;
mtshi.insert(x);
sumhi += x;
}
while (isz(mtslo) < isz(mtshi)){
int x = *mtshi.begin();
mtshi.erase(mtshi.find(x));
sumhi -= x;
mtslo.insert(x);
sumlo += x;
}
}
void insert(int x){
if (x <= median()){
mtslo.insert(x);
sumlo += x;
}
else{
mtshi.insert(x);
sumhi += x;
}
balance();
}
void erase(int x){
if (mtslo.find(x) != mtslo.end()){
mtslo.erase(mtslo.find(x));
sumlo -= x;
}
else{
mtshi.erase(mtshi.find(x));
sumhi -= x;
}
balance();
}
int besthub(int _n, int l, int _a[], ll val){
n = _n;
For(i, 0, n){
a[i] = _a[i];
}
int ans = 0;
int r = -1;
For(l, 0, n){
while (r + 1 < n){
r++;
insert(a[r]);
if (cal_dist() > val){
erase(a[r]);
r--;
break;
}
}
ans = max(ans, r - l + 1);
erase(a[l]);
}
return ans;
}
/*
==================================================+
INPUT |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
304 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 |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
304 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 |
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 |
308 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 |
232 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 |
312 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 |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
312 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 |
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 |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
572 KB |
Output is correct |
25 |
Correct |
3 ms |
572 KB |
Output is correct |
26 |
Correct |
4 ms |
596 KB |
Output is correct |
27 |
Correct |
3 ms |
484 KB |
Output is correct |
28 |
Correct |
3 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
596 KB |
Output is correct |
2 |
Correct |
8 ms |
596 KB |
Output is correct |
3 |
Correct |
59 ms |
5968 KB |
Output is correct |
4 |
Correct |
59 ms |
5964 KB |
Output is correct |
5 |
Correct |
24 ms |
924 KB |
Output is correct |
6 |
Correct |
21 ms |
924 KB |
Output is correct |
7 |
Correct |
57 ms |
5924 KB |
Output is correct |
8 |
Correct |
57 ms |
5964 KB |
Output is correct |
9 |
Correct |
26 ms |
1200 KB |
Output is correct |
10 |
Correct |
38 ms |
1220 KB |
Output is correct |
11 |
Correct |
57 ms |
5964 KB |
Output is correct |
12 |
Correct |
58 ms |
5956 KB |
Output is correct |
13 |
Correct |
26 ms |
944 KB |
Output is correct |
14 |
Correct |
24 ms |
996 KB |
Output is correct |
15 |
Correct |
47 ms |
4552 KB |
Output is correct |
16 |
Correct |
42 ms |
4568 KB |
Output is correct |
17 |
Correct |
51 ms |
5348 KB |
Output is correct |
18 |
Correct |
51 ms |
5388 KB |
Output is correct |
19 |
Correct |
59 ms |
5728 KB |
Output is correct |
20 |
Correct |
55 ms |
5688 KB |
Output is correct |