#include "ricehub.h"
#include <iostream>
#include <set>
#include <vector>
#define ll long long
#define ff first
#define ss second
#define ln "\n"
using namespace std;
ll n;
int besthub(int R, int L, int X[], long long B)
{
n=R;
vector<ll> a(n);
for (ll i=0; i<n; i++){
a[i]=X[i];
}
ll l=0, r=0;
ll ans=1, lsum=0, rsum=a[0];
multiset<ll> left;
multiset<ll> right;
right.insert(a[0]);
while (r<n){
// cout << l << " " << r << endl;
ll cmed = (*right.begin());
if ((ll)(cmed*left.size()-lsum+rsum-cmed*(right.size()))<=(ll)B){
ans=max(ans, r-l+1);
r++;
ll nwl=r-l+1;
if (r==n) break;
if (left.empty() or a[r]>(*left.begin())){
right.insert(a[r]);
rsum+=a[r];
}else{
left.insert(a[r]);
lsum+=a[r];
}
while ((ll)right.size()>nwl-nwl/2){
rsum-=*right.begin();
lsum+=*right.begin();
left.insert(*right.begin());
right.erase(right.begin());
}
while (left.size()>right.size()){
lsum-=*left.rbegin();
rsum+=*left.rbegin();
right.insert(*left.rbegin());
left.erase(left.find(*left.rbegin()));
}
}else{
// cout << "H" << endl;
if (left.find(a[l])!=left.end()){
lsum-=a[l];
left.erase(left.find(a[l]));
}else{
rsum-=a[l];
right.erase(right.find(a[l]));
}
l++;
ll nwl=r-l+1;
while ((ll)right.size()>nwl-nwl/2){
rsum-=*right.begin();
lsum+=*right.begin();
left.insert(*right.begin());
right.erase(right.begin());
}
while (left.size()>right.size()){
lsum-=*left.rbegin();
rsum+=*left.rbegin();
right.insert(*left.rbegin());
left.erase(left.find(*left.rbegin()));
}
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
344 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
600 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
448 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
460 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 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 |
392 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 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 |
720 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
712 KB |
Output is correct |
2 |
Correct |
3 ms |
604 KB |
Output is correct |
3 |
Correct |
26 ms |
9176 KB |
Output is correct |
4 |
Correct |
26 ms |
9052 KB |
Output is correct |
5 |
Correct |
8 ms |
3164 KB |
Output is correct |
6 |
Correct |
8 ms |
3160 KB |
Output is correct |
7 |
Correct |
27 ms |
8692 KB |
Output is correct |
8 |
Correct |
26 ms |
8784 KB |
Output is correct |
9 |
Correct |
11 ms |
3416 KB |
Output is correct |
10 |
Correct |
12 ms |
3420 KB |
Output is correct |
11 |
Correct |
26 ms |
9040 KB |
Output is correct |
12 |
Correct |
36 ms |
9176 KB |
Output is correct |
13 |
Correct |
9 ms |
3420 KB |
Output is correct |
14 |
Correct |
9 ms |
3420 KB |
Output is correct |
15 |
Correct |
19 ms |
7520 KB |
Output is correct |
16 |
Correct |
19 ms |
7512 KB |
Output is correct |
17 |
Correct |
23 ms |
8232 KB |
Output is correct |
18 |
Correct |
23 ms |
8536 KB |
Output is correct |
19 |
Correct |
24 ms |
8784 KB |
Output is correct |
20 |
Correct |
24 ms |
8764 KB |
Output is correct |