#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--;
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;
int 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 - 1)
{
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;
if(rice_cnt >= m && price <= B) {
ans = max(ans, rice_cnt);
valid = true;
}
rice_cnt -= road[beginidx].second;
left_cnt -= road[beginidx].second;
tempprice -= (road[mid].first - 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Execution timed out |
1084 ms |
212 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1144 KB |
Output is correct |
2 |
Correct |
8 ms |
1064 KB |
Output is correct |
3 |
Correct |
28 ms |
3904 KB |
Output is correct |
4 |
Correct |
29 ms |
3984 KB |
Output is correct |
5 |
Execution timed out |
1080 ms |
832 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |