#include "ricehub.h"
using namespace std;
#include <bits/stdc++.h>
#define ll long long
ll countPsum[1000007], sumPsum[1000007];
ll countrq(ll a, ll b){
if (b < a) return 0;
if (a == 0) return countPsum[b];
else return countPsum[b] - countPsum[a-1];
}
ll sumrq(ll a, ll b){
if (b < a) return 0;
if (a == 0) return sumPsum[b];
else return sumPsum[b] - sumPsum[a-1];
}
int besthub(int R, int L, int X[], long long B)
{
vector<pair<ll, ll>> vec;
for (int x = 0; x < R; x++){
if (vec.size() == 0 || vec.back().first != X[x]) vec.push_back({X[x], 1});
else vec.back().second++;
}
ll n = vec.size();
countPsum[0] = vec[0].second;
sumPsum[0] = vec[0].first * vec[0].second;
for (int x = 1; x < n; x++){
countPsum[x] = countPsum[x-1] + vec[x].second;
sumPsum[x] = sumPsum[x-1] + vec[x].first * vec[x].second;
}
ll l = 0, r = 0, median = 0; ll ans = 1;
ll leftSize = 0, rightSize = 0;
while (r != n){
while (abs(rightSize - leftSize) > abs(rightSize - leftSize - 2*vec[median].second)){
rightSize -= vec[median].second; leftSize += vec[median].second; median++;
}
while (abs(rightSize - leftSize) > abs(rightSize - leftSize + 2*vec[median].second)){
rightSize += vec[median].second; leftSize -= vec[median].second; median--;
}
ll cost = countrq(l, median-1) * vec[median].first - sumrq(l, median-1)
+ sumrq(median+1, r) - countrq(median+1, r) * vec[median].first;
;
if (cost > B) leftSize -= vec[l].second, l++;
else ans = max(r-l+1, ans), r++, rightSize += vec[r].second;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1100 KB |
Output is correct |
2 |
Correct |
3 ms |
1100 KB |
Output is correct |
3 |
Correct |
18 ms |
3908 KB |
Output is correct |
4 |
Correct |
15 ms |
4988 KB |
Output is correct |
5 |
Incorrect |
7 ms |
812 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |