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"
using namespace std;
typedef long long ll;
const int N = 100000 + 7;
int n, len, x[N];
ll b;
deque<int> positions;
deque<int> first, second;
ll compute() {
int x = positions[(int) positions.size() / 2], y;
if ((int) second.size() == (int) first.size()) {
//y = first.back();
y = x;
} else {
/// cout << (int) first.size() << " and " << (int) second.size() << "\n";
assert((int) second.size() == (int) first.size() - 1);
y = x;
}
assert(x == y);
ll cost = 0, sum = 0;
for (auto &p : positions) {
cost += abs(x - p);
}
for (auto &p : first) {
sum += x - p;
}
for (auto &p : second) {
sum += p - x;
}
assert(sum == cost);
return cost;
}
int besthub(int r, int dim, int rice_fields[], ll gold_coins) {
n = r;
len = dim;
for (int i = 1; i <= n; i++) {
x[i] = rice_fields[i - 1];
}
b = gold_coins;
int sol = 0;
for (int i = 1; i <= n; i++) {
second.push_back(x[i]);
if ((int) second.size() > (int) first.size()) {
first.push_back(second.front());
second.pop_front();
}
positions.push_back(x[i]);
while (compute() > b) {
positions.pop_front();
if (first.empty()) {
second.pop_front();
} else {
first.pop_front();
}
if ((int) second.size() > (int) first.size()) {
first.push_back(second.front());
second.pop_front();
}
}
sol = max(sol, (int) positions.size());
}
return sol;
}
# | 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... |