#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 = 0;
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);
//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;
ll prev = 0;
if(mid != -1)
prev = road[mid].first;
tempprice -= (prev - road[beginidx].first) * road[beginidx].second;
}
if(valid)
left = m + 1;
else
right = m - 1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
1 ms |
256 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
976 KB |
Output is correct |
2 |
Correct |
5 ms |
976 KB |
Output is correct |
3 |
Correct |
26 ms |
2900 KB |
Output is correct |
4 |
Correct |
24 ms |
2820 KB |
Output is correct |
5 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |