#include "ricehub.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
template<typename T>
int len(T &a){return a.size();}
int besthub(int n, int lim, int a[], long long cost)
{
int ans = 0;
multiset<int> lef, rig;
ll sl = 0, sr = 0;
ll mon = 0;
auto add = [&](int x){
if(lef.empty() || *lef.rbegin() >= x){
lef.insert(x);
sl += x;
if(len(lef) - 1 >= len(rig) + 1){
int m = *lef.rbegin();
lef.erase(lef.find(m));
sl -= m;
rig.insert(m);
sr += m;
}
}else{
rig.insert(x);
sr += x;
if(len(rig) > len(lef)){
ll m = *rig.begin();
rig.erase(rig.begin());
sr -= m;
lef.insert(m);
sl += m;
}
}
if(lef.empty()) mon = 0;
else{
ll m = *lef.rbegin();
mon = m * len(lef) - sl + sr - m * len(rig);
}
};
auto rem = [&](int x){
if(x > *lef.rbegin()){
rig.erase(rig.find(x));
sr -= x;
if(len(lef) - 1 >= len(rig) + 1){
int m = *lef.rbegin();
lef.erase(lef.find(m));
sl -= m;
rig.erase(m);
sr += m;
}
}else{
lef.erase(lef.find(x));
sl -= x;
if(len(rig) > len(lef)){
ll m = *rig.begin();
rig.erase(rig.begin());
sr -= m;
lef.insert(m);
sl += m;
}
}
if(lef.empty()) mon = 0;
else{
ll m = *lef.rbegin();
mon = m * len(lef) - sl + sr - m * len(rig);
}
};
for(int l = 0, r = 0; r < n; r ++){
add(a[r]);
while(mon > cost){
rem(a[l]);
l ++;
}
if(mon <= cost) ans = max(ans, r - l + 1);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
444 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
524 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
29 ms |
5504 KB |
Output is correct |
4 |
Correct |
30 ms |
5280 KB |
Output is correct |
5 |
Correct |
8 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
860 KB |
Output is correct |
7 |
Correct |
29 ms |
6180 KB |
Output is correct |
8 |
Correct |
27 ms |
6236 KB |
Output is correct |
9 |
Correct |
11 ms |
1116 KB |
Output is correct |
10 |
Correct |
11 ms |
1228 KB |
Output is correct |
11 |
Correct |
29 ms |
6416 KB |
Output is correct |
12 |
Correct |
30 ms |
6484 KB |
Output is correct |
13 |
Correct |
9 ms |
860 KB |
Output is correct |
14 |
Correct |
9 ms |
872 KB |
Output is correct |
15 |
Correct |
20 ms |
4808 KB |
Output is correct |
16 |
Correct |
21 ms |
4840 KB |
Output is correct |
17 |
Correct |
26 ms |
5868 KB |
Output is correct |
18 |
Correct |
26 ms |
5876 KB |
Output is correct |
19 |
Correct |
28 ms |
6120 KB |
Output is correct |
20 |
Correct |
28 ms |
6228 KB |
Output is correct |