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 "books.h"
using namespace std;
//
// --- Sample implementation for the task books ---
//
// To compile this program with the sample grader, place:
// books.h books_sample.cpp sample_grader.cpp
// in a single folder and run:
// g++ books_sample.cpp sample_grader.cpp
// in this folder.
//
void solve(int N, int K, long long A, int S) {
// first K - 1 books
// and first one >= A
// >= 2 * A -> impossible
std::vector<long long> mem(N, -1);
auto ask = [&](int i) {
assert(0 <= i && i < N);
if (mem[i] != -1) return mem[i];
return mem[i] = skim(i + 1);
};
long long sum = 0;
for (int i = 0; i < K; i++) {
sum += ask(i);
}
if (sum > 2 * A) {
impossible();
return;
}
if (sum >= A) {
std::vector<int> res(K);
std::iota(res.begin(), res.end(), 1);
answer(res);
return;
}
// sum < A now
int lb = K, rb = N;
while (lb < rb) {
int mid = (lb + rb) / 2;
if (ask(mid) > A) {
rb = mid;
} else {
lb = mid + 1;
}
}
int id = rb;
if (id < N) {
auto ssum = sum + ask(id) - ask(K - 1);
if (ssum >= A && ssum <= 2 * A) {
std::vector<int> res;
for (int i = 0; i < K - 1; i++) {
res.emplace_back(i + 1);
}
res.emplace_back(id + 1);
answer(res);
return;
}
}
std::vector<int> res_right;
for (int i = id - 1; i >= std::max(K, id - K); i--) {
res_right.emplace_back(i);
ask(i);
}
// we know first sol is not working
std::vector<int> res_left;
for (int i = 0; i < K; i++) {
res_left.push_back(i);
}
while (res_right.size()) {
sum -= ask(res_left.front());
res_left.erase(res_left.begin());
sum += ask(res_right.back());
res_left.emplace_back(res_right.back());
res_right.pop_back();
if (sum >= A) {
// we good
for (int& x : res_left) x++;
answer(res_left);
return;
}
}
impossible();
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |