This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ricehub.h>
#define DEBUG 0
using namespace std;
int cost(int R, int X[], int mid, long long B) {
long long closest = 1e18, sum = 0;
int pos = 0, l, r, ct = 0, ok;
for(int i = 0; i < R; i++) {
if(abs(X[i] - mid) < closest) {
closest = abs(X[i] - mid);
pos = i;
}
}
if(X[pos] <= mid) {
r = min(R - 1, pos + 1);
l = r - 1;
}
else {
l = max(0, pos - 1);
r = l + 1;
}
while(1) {
closest = 1e18;
ok = -1;
if(l >= 0 && closest > abs(X[l] - mid)) {
closest = abs(X[l] - mid);
ok = 0;
}
if(r < R && closest > abs(X[r] - mid)) {
closest = abs(X[r] - mid);
ok = 1;
}
if(sum + closest > B) {
return ct;
}
ct++;
sum += closest;
if(!ok) {
l--;
}
else {
r++;
}
}
return R;
}
int besthub(int R, int L, int X[], long long B) {
int l = 1, r = L, ans = 0;
while(l <= r) {
int mid = (l + r) / 2;
int cost1 = cost(R, X, mid, B);
int l2 = l + 1, r2 = r, pos2 = r;
while(l2 <= r2) {
int mid2 = (l2 + r2) / 2;
int cost2 = cost(R, X, mid2, B);
if(cost1 == cost2) {
l2 = mid2 + 1;
}
else {
r2 = mid2 - 1;
pos2 = min(pos2, mid2);
}
}
int cost2 = cost(R, X, pos2, B);
if(cost1 > cost2) {
r = mid - 1;
ans = max(ans, cost1);
}
else {
l = mid + 1;
ans = max(ans, cost2);
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |