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;
void solve(int n, int k, long long A, int S) {
if (S == n) {
long long a[n + 1];
for (int i = 1; i <= n; i++)
a[i] = skim(i);
long long pref[n + 1];
pref[0] = 0;
for (int i = 1; i <= n; i++)
pref[i] = a[i] + pref[i - 1];
function<long long(int, int)> sum = [&](int l, int r) {
return pref[r] - pref[l - 1];
};
for (int i = 1; i <= n; i++) {
if (sum(i, i + k - 1) >= A && sum(i, i + k - 1) <= 2 * A) {
vector<int> ans;
for (int j = i; j < i + k; j++)
ans.emplace_back(j);
answer(ans);
return;
}
};
long long s[n + 1];
for (int i = 1; i + k - 2 <= n; i++)
s[i] = sum(i, i + k - 2);
for (int i = k; i <= n; i++) {
int l = 1, r = i - k + 1;
while (r - l > 1) {
int m = (l + r) >> 1;
if (s[m] + a[i] >= A)
r = m;
else
l = m;
}
int ind = -1;
if (s[l] + a[i] >= A)ind = l;
else if (s[r] + a[i] >= A)ind = r;
if (ind != -1 && s[ind] + a[i] <= 2 * A) {
vector<int> ans;
for (int j = ind; j < ind + k - 1; j++)
ans.emplace_back(j);
ans.emplace_back(i);
answer(ans);
return;
}
}
impossible();
return;
}
long long sum = 0;
vector<pair<int, long long>> ans;
map<int, long long> mp;
function<long long(int)> skimm = [&](int i) {
if (mp.find(i) != mp.end())
return mp[i];
long long s = skim(i);
mp[i] = s;
return s;
};
for (int i = 1; i <= k; i++) {
long long t = skimm(i);
sum += t;
ans.emplace_back(i, t);
}
for (int j = k; j >= 1; j--) {
if (sum >= A)break;
int l = ans[j - 1].first, r = (j < k ? ans[j].first - 1 : n);
long long val = ans[j - 1].second;
long long X = skimm(r);
if (sum + X <= 2 * A) {
ans[j - 1].first = r;
sum -= ans[j - 1].second;
ans[j - 1].second = X;
sum += ans[j - 1].second;
continue;
}
while (r - l > 1) {
int m = (l + r) / 2;
if (sum - val + skimm(m) >= A)
r = m;
else
l = m;
}
long long s1 = skimm(l), s2 = skimm(r);
if (sum - val + s2 > 2 * A || sum - val + s1 >= A)
ans[j - 1].first = l, ans[j - 1].second = s1,
sum = sum - val + s1;
else
ans[j - 1].first = r, ans[j - 1].second = s2,
sum = sum - val + s2;
}
if (sum > 2 * A || sum < A)
impossible();
else {
vector<int> res;
for (int i = 0; i < k; i++)
res.emplace_back(ans[i].first);
answer(res);
}
}
# | 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... |