#include "ricehub.h"
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int besthub(int R, int L, int X[], long long B)
{
vector< pair<ll, ll> > road;
for(int i = 0; i < R; i++)
{
int idx = i + 1, cnt = 1;
while(idx < R && X[idx] == X[i])
{
cnt++;
idx++;
}
idx--;
road.push_back({X[i], cnt});
i = idx;
}
int N = road.size();
int left = 1, right = R;
int ans = 0;
while(left <= right)
{
int m = (left + right) / 2; // We are controlling whether we can get m rices. We use binary search because rice count is monotonic. For example, we can get 6 rices, if we can get 7 rices.
//int mid = 1;
bool valid = false;
ll price = 1e18, tempprice = 0;
int rice_cnt = 0;
ll left_cnt = 0, right_cnt = 0;
int endidx = -1, mid = -1;
for(int beginidx = 0; beginidx < N; beginidx++)
{
ll curprice = 1e18;
while(endidx < N && rice_cnt < m)
{
endidx++;
rice_cnt += road[endidx].second;
right_cnt += road[endidx].second;
ll prev = -1; // Min -1 because when it becomes 0 -> it might interfere with elements at 0th coordinate
if(mid != -1)
prev = road[mid].first;
tempprice += road[endidx].second * (road[endidx].first - prev);
//endidx++;
}
//endidx--;
int mini = 0;
price = min(price, tempprice);
curprice = min(tempprice, curprice);
if(curprice == tempprice)
mini = mid;
while(left_cnt < right_cnt && mid < endidx)
{
mid++;
if(mid == 0) {
tempprice -= right_cnt * (road[mid].first + 1);
//tempprice += left_cnt * road[mid].first;
}
else {
tempprice -= right_cnt * (road[mid].first - road[mid - 1].first);
tempprice += left_cnt * (road[mid].first - road[mid - 1].first);
}
right_cnt -= road[mid].second;
left_cnt += road[mid].second;
price = min(price, tempprice);
curprice = min(tempprice, curprice);
if(curprice == tempprice)
mini = mid;
}
mid = mini;
tempprice = curprice;
if(rice_cnt >= m && tempprice <= B) {
ans = max(ans, rice_cnt);
valid = true;
}
rice_cnt -= road[beginidx].second;
left_cnt -= road[beginidx].second;
if(mid != -1)
tempprice -= (road[mid].first - road[beginidx].first) * road[beginidx].second;
else
tempprice -= (road[beginidx].first + 1) * road[beginidx].second;
}
if(valid)
left = m + 1;
else
right = m - 1;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
976 KB |
Output is correct |
2 |
Correct |
5 ms |
976 KB |
Output is correct |
3 |
Correct |
24 ms |
2888 KB |
Output is correct |
4 |
Correct |
26 ms |
2824 KB |
Output is correct |
5 |
Incorrect |
5 ms |
488 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |