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 <iostream>
#include <vector>
#include <set>
using namespace std;
#include "books.h"
int findA(vector<long long>& books, long long A, int lower, int upper) {
if (books[lower] == -1) {
books[lower] = skim(lower + 1);
}
if (books[upper] == -1) {
books[upper] = skim(upper + 1);
}
if (books[lower] > A) {
return -1;
}
if (books[upper] <= A) {
return upper;
}
if (upper - lower < 2) {
return lower;
}
int half = (upper - lower + 1) / 2;
books[lower + half] = skim(lower + half + 1);
if (books[lower + half] <= A) return findA(books, A, lower + half, upper);
else return findA(books, A, lower, lower + half);
}
void solve(int N, int K, long long A, int S) {
if (K > N) {
impossible();
return;
}
vector<long long> books(N);
for (int i = 0; i < N; i++) {
books[i] = -1;
}
long long sumFirstK = 0;
for (int i = 0; i < K; i++) {
if (books[i] == -1) {
books[i] = skim(i + 1);
}
sumFirstK += books[i];
}
if (sumFirstK > A * 2) {
impossible();
}
else {
vector<int> res;
for (int i = 0; i < K; i++) {
res.push_back(i + 1);
}
if (sumFirstK >= A) {
answer(res);
}
else {
// sumFirstK < A
// find last element < A
long long posA = findA(books, A, 0, N - 1);
long long currentSum = sumFirstK;
vector<int> attempt(res);
// try to replace with largest values < A
for (int i = 0; i < K; i++) {
if (posA - i < 0) {
break;
}
if (books[posA - i] == -1) {
books[posA - i] = skim(posA - i + 1);
}
currentSum -= books[K - 1 - i];
attempt[K - 1 - i] = posA - i + 1;
currentSum += books[posA - i];
if ((currentSum >= A) && (currentSum <= 2 * A)) {
// check that no book appears twice
set<int> validator;
for (int j = 0; j < K; j++) {
validator.insert(attempt[j]);
}
if (validator.size() == K) {
answer(attempt);
return;
}
break;
}
}
// failed to build the sum with elements < A only
// need to look at elements > A
int j = 0;
while ((j < K) && ((posA + 1 + j) < N)) {
if (books[posA + j + 1] == -1) {
books[posA + j + 1] = skim(posA + j + 2);
}
sumFirstK -= books[K - j - 1];
res[K - j - 1] = posA + j + 2;
sumFirstK += books[posA + j + 1];
if (sumFirstK > A * 2) {
impossible();
return;
}
else {
if (sumFirstK >= A) {
set<int> validator;
for (int l = 0; l < K; l++) {
validator.insert(res[l]);
}
if (validator.size() == K) {
answer(res);
}
else {
impossible();
}
break;
}
}
++j;
}
impossible();
}
}
return;
}
Compilation message (stderr)
books.cpp: In function 'void solve(int, int, long long int, int)':
books.cpp:86:27: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
86 | if (validator.size() == K) {
| ~~~~~~~~~~~~~~~~~^~~~
books.cpp:114:28: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | if (validator.size() == K) {
| ~~~~~~~~~~~~~~~~~^~~~
# | 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... |