/**
* author: feev1x
* created: 10.01.2025 10:20:42
**/
#include <bits/stdc++.h>
constexpr int64_t INF = 1e18;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n; std::cin >> n;
int64_t sl, sc, el, ec; std::cin >> sl >> sc >> el >> ec;
std::vector<int64_t> a(n + 1), suf_min(n + 2, INF);
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
}
int64_t res = INF;
if (sl > el) {
int64_t cost_1 = INF;
for (int i = el; i <= sl; ++i) {
suf_min[i] = std::min(suf_min[i - 1], a[i]);
if (i == el) {
continue;
}
if (a[i] < sc) {
cost_1 = 0;
} else {
cost_1 = std::min(cost_1, a[i] - sc + 1);
}
}
res = std::min(res, el - sl + cost_1 + ec - 1);
res = std::min(res, el - sl + std::abs(std::min(suf_min[sl] + 1, sc) - ec));
for (int i = sl; i >= el; --i) {
sc = std::min(sc, a[i] + 1);
res = std::min(res, el - sl + std::abs((suf_min[i] + 1) - ec) + sc + 1);
}
} else {
int64_t cost_1 = INF;
for (int i = el; i >= sl; --i) {
suf_min[i] = std::min(suf_min[i + 1], a[i]);
if (a[i] < sc) {
cost_1 = 0;
} else {
cost_1 = std::min(cost_1, a[i] - sc + 1);
}
}
res = std::min(res, el - sl + cost_1 + ec);
res = std::min(res, el - sl + std::abs(std::min(suf_min[sl] + 1, sc) - ec));
for (int i = sl; i < el; ++i) {
sc = std::min(sc, a[i] + 1);
res = std::min(res, el - sl + std::abs((suf_min[i + 1] + 1) - ec) + sc);
}
}
std::cout << res << '\n';
return 0;
}
# | 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... |