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;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5+10;
const int X = 12;
ll n, k, a, s;
bool us[N];
ll val[N];
ll ask(int x) {
if (us[x]) return val[x];
us[x]=true;
return val[x]=skim(x);
}
bool check(vector<int> v) {
ll sm=0;
for (auto u : v) sm+=ask(u);
return a <= sm && sm <= 2*a;
}
void solve(int _n, int _k, ll _a, int _s) {
n=_n, k=_k, a=_a, s=_s;
ll sm=0;
vector<int> v;
for (int i=1; i<k; ++i) v.push_back(i), sm+=ask(i);
if (sm+ask(k) > 2*a) {
impossible();
return;
} else if (a <= sm+ask(k) && sm+ask(k) <= 2*a) {
v.push_back(k);
answer(v);
return;
} sm+=ask(k);
int l=k, r=n, res=l;
while (l <= r) {
int m=(l+r)/2;
if (ask(m) <= a) res=m, l=m+1;
else r=m-1;
}
set<int> sx;
for (int i=1; i<=k; ++i) sx.insert(i);
if (res+1 <= n && sm-ask(k)+ask(res+1) <= 2*a && sm-ask(k)+ask(res+1) >= a) {
sx.erase(k), sx.insert(res+1);
answer(vector<int>(sx.begin(), sx.end()));
return;
}
int lp=k, rp=res;
while (sm < a && lp > 0) {
if (sm-ask(lp)+ask(rp) <= 2*a) {
sm=sm-ask(lp)+ask(rp);
sx.erase(lp), sx.insert(rp);
--lp, --rp;
} else {
int lf=lp, rf=rp, res=lp;
while (lf <= rf) {
int m=(lf+rf)/2;
if (sm-ask(lp)+ask(m) <= 2*a) res=m, lf=m+1;
else rf=m-1;
}
sx.erase(lp), sx.insert(res);
sm=sm-ask(lp)+ask(res);
assert(a <= sm && sm <= 2*a);
vector<int> v(sx.begin(), sx.end());
answer(v);
}
}
if (sm >= a && sm <= 2*a) answer(vector<int>{sx.begin(), sx.end()});
else 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... |