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;
#define int long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
//
// --- 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.
//
const int maxn = 1e5+10;
const int maxv = 2e18+10;
int n, b[maxn];
int cnt;
int qry(int32_t i) {
if(b[i] == 0) {
assert(cnt--);
return b[i] = skim(i);
}
return b[i];
}
int qrysum(int l, int r) {
int sum = 0;
for(int i = l; i <= r; i++) {
sum+= qry(i);
sum = min(sum,maxv);
}
return sum;
}
void solve(int32_t N, int32_t k, long long A, int32_t S) {
cnt = 50;
n = N;
int l,r;
l = 1;
r = n;
int p = -1;
while(l <= r) {
int mid = (l+r)/2;
if(qry(mid) >= A) {
r = mid-1;
p = mid;
}
else {
l = mid+1;
}
}
if(p != -1) {
if(p < k) {
impossible();
return;
}
if(qrysum(1,k-1)+qry(p) <= 2*A) {
vector<int32_t> ans;
for(int j = 1; j <= k-1; j++) ans.pb(j);
ans.pb(p);
answer(ans);
return;
}
if(p == k) {
impossible();
return;
}
n = p-1;
// n is >= k
}
// l = 1;
// r = n-k+1;
// while(l <= r) {
// int mid = (l+r)/2;
// if(qrysum(mid,mid+k-1) < A) {
// l = mid+1;
// }
// else if(qrysum(mid,mid+k-1) > 2*A) {
// r = mid-1;
// }
// else {
// vector<int32_t> ans;
// for(int j = mid; j <= mid+k-1; j++) ans.pb(j);
// answer(ans);
// return;
// }
// }
int sumcur = qrysum(1,k);
if(sumcur > 2*A) {
impossible();
return;
}
if(sumcur >= A) {
vector<int32_t> ans;
for(int j = 1; j <= k; j++) ans.pb(j);
answer(ans);
return;
}
vector<int32_t> ans;
for(int i = k; i >= 1; i--) {
if(sumcur-qry(i)+qry(n) < A) {
sumcur = sumcur-qry(i)+qry(n);
ans.pb(n);
n--;
}
else if(sumcur-qry(i)+qry(n) <= 2*A) {
//I found the answer
ans.pb(n);
for(int j = 1; j < i; j++) ans.pb(j);
sort(all(ans));
answer(ans);
return;
}
else {
l = i;
r = n;
while(l <= r) {
int mid = (l+r)/2;
if(sumcur-qry(i)+qry(mid) > 2*A) {
r = mid-1;
}
else if(sumcur-qry(i)+qry(mid) < A) {
l = mid+1;
}
else {
ans.pb(mid);
for(int j = 1; j < i; j++) ans.pb(j);
sort(all(ans));
answer(ans);
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... |