이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n, s;
ll k;
std::cin >> n >> s >> k;
std::vector<ll> d(n - 1);
for (auto &i : d) {
std::cin >> i;
}
std::vector<ll> v(n);
for (auto &i : v) {
std::cin >> i;
}
std::vector<std::vector<ll>> lift(n, std::vector<ll>(21));
std::vector<ll> right(n);
ll dist = 0;
for (ll i = n - 1, j = n - 1; i >= 0; --i) {
if (i != n - 1) {
dist += d[i];
}
// j is the last index satisfying dist[i...j] <= k
while (dist > k and j - 1 >= 0) {
dist -= d[j - 1];
j--;
}
lift[i][0] = std::min(n - 1, j + 1);
right[i] = j;
}
dist = 0;
std::vector<ll> left(n);
for (ll i = 0, j = 0; i < n; ++i) {
if (i != 0) {
dist += d[i - 1];
}
while (dist > k and j < n - 1) {
dist -= d[j];
j++;
}
left[i] = j;
}
for (ll bt = 1; bt <= 20; ++bt) {
for (ll i = 0; i < n; ++i) {
lift[i][bt] = lift[lift[i][bt - 1]][bt - 1];
}
}
auto binary_lift = [&](ll node, ll k) {
for (ll bt = 0; bt <= 20; ++bt) {
if (k & (1 << bt)) {
node = lift[node][bt];
}
}
return node;
};
std::vector<ll> psum(n);
std::partial_sum(v.begin(), v.end(), psum.begin());
auto sum = [&](ll l, ll r) {
ll ans = psum[r];
if (l - 1 >= 0) {
ans -= psum[l - 1];
}
return ans;
};
std::pair<ll, ll> best = {0, 0};
for (ll i = 0; i < n; ++i) {
best = std::max(
best, std::make_pair(sum(left[i], right[binary_lift(i, s - 1)]), i));
}
std::vector<ll> ans;
for (ll i = 0, node = best.second; i < s; ++i) {
ans.push_back(node);
node = lift[node][0];
}
while (ans.size() >= 2 and ans.back() == ans[ans.size() - 2]) {
ans.pop_back();
}
std::cout << ans.size() << "\n";
for (auto &i : ans) {
std::cout << i + 1 << " ";
}
std::cout << "\n";
}
# | 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... |