# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
486217 | Jomnoi | 쌀 창고 (IOI11_ricehub) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ricehub.h>
#define DEBUG 0
using namespace std;
long long cost(int n, long long X[], long long mid, long long B) {
deque <long long> L, R;
for(int i = 0; i < n; i++) {
if(X[i] <= mid) {
L.push_front(mid - X[i]);
}
else {
R.push_back(X[i] - mid);
}
}
long long i = 0, j = 0;
long long sum = 0;
while(i < L.size() && j < R.size()) {
if(L[i] <= R[j]) {
sum += L[i++];
}
else {
sum += R[j++];
}
if(sum > B) {
return i + j - 1;
}
}
while(i < L.size()) {
sum += L[i++];
if(sum > B) {
return i + j - 1;
}
}
while(j < R.size()) {
sum += R[j++];
if(sum > B) {
return i + j - 1;
}
}
return n;
}
int besthub(int R, long long L, long long X[], long long B) {
long long l = 1, r = L, ans = 0, mid, cost1;
long long l2, r2, mid2, pos2, cost2;
while(l <= r) {
mid = (l + r) / 2;
cost1 = cost(R, X, mid, B);
l2 = l + 1, r2 = r, pos2 = r;
while(l2 <= r2) {
mid2 = (l2 + r2) / 2;
if(cost1 == cost(R, X, mid2, B)) {
l2 = mid2 + 1;
}
else {
r2 = mid2 - 1;
pos2 = min(pos2, mid2);
}
}
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;
}