Submission #71503889
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(arg) cout << "[" << #arg << "]: " << arg << endl
#else
#define debug(arg) 42
#endif
using llu = uint64_t;
using ll = int64_t;
#define vec vector
#define pb push_back
#define all(n) begin(n), end(n)
struct Tree {
int n;
vec<int> s;
Tree(int n, vec<int> const& p) : n(n), s(2*n) {
copy(all(p), s.begin() + n);
for (int i = n - 1; i; --i) s[i] = max(s[i<<1], s[i<<1|1]);
}
int get(int l, int r) {
int mx = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) mx = max(mx, s[l++]);
if (r&1) mx = max(mx, s[--r]);
}
return mx;
}
};
void solv() {
int n; cin >> n;
vec<int> p(n); for (auto &i : p) cin >> i, --i;
vec<int> ip(n); for (int i = 0; i < n; ++i) ip[p[i]] = i;
Tree tree(n, p);
function<pair<int, ll>(int, int)> process = [&](int l, int r) -> pair<int, ll> {
if (r <= l || l < 0 || r > n) return {0, -1ll};
int mx = tree.get(l, r);
int mxp = ip[mx];
auto [mxlp, lc] = process(l, mxp);
auto [mxrp, rc] = process(mxp + 1, r);
ll ans = 0;
if (lc != -1) ans = max(ans, lc + abs(mxp - mxlp));
if (rc != -1) ans = max(ans, rc + abs(mxp - mxrp));
return {mxp, ans};
};
cout << process(0, n).second << '\n';;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int t = 1;
//cin >> t;
while (t--) solv();
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | F - Cat exercise |
| User | fisher199 |
| Language | C++23 (Clang 21.1.0) |
| Score | 550 |
| Code Size | 1420 Byte |
| Status | AC |
| Exec Time | 84 ms |
| Memory | 18524 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 550 / 550 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | example_00.txt, example_01.txt |
| All | example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| example_00.txt | AC | 1 ms | 2960 KiB |
| example_01.txt | AC | 1 ms | 3056 KiB |
| hand_00.txt | AC | 84 ms | 18496 KiB |
| hand_01.txt | AC | 83 ms | 18524 KiB |
| hand_02.txt | AC | 79 ms | 12236 KiB |
| hand_03.txt | AC | 81 ms | 17228 KiB |
| hand_04.txt | AC | 73 ms | 12188 KiB |
| hand_05.txt | AC | 1 ms | 3000 KiB |
| hand_06.txt | AC | 1 ms | 2888 KiB |
| hand_07.txt | AC | 64 ms | 6084 KiB |
| random_00.txt | AC | 70 ms | 6036 KiB |
| random_01.txt | AC | 70 ms | 6092 KiB |
| random_02.txt | AC | 70 ms | 6128 KiB |
| random_03.txt | AC | 71 ms | 6140 KiB |
| random_04.txt | AC | 70 ms | 6112 KiB |
| random_05.txt | AC | 70 ms | 6076 KiB |
| random_06.txt | AC | 70 ms | 6088 KiB |
| random_07.txt | AC | 71 ms | 6128 KiB |
| random_08.txt | AC | 70 ms | 6084 KiB |
| random_09.txt | AC | 70 ms | 6084 KiB |
| random_10.txt | AC | 75 ms | 10688 KiB |
| random_11.txt | AC | 74 ms | 9804 KiB |
| random_12.txt | AC | 71 ms | 6800 KiB |
| random_13.txt | AC | 72 ms | 8780 KiB |
| random_14.txt | AC | 80 ms | 14796 KiB |
| random_15.txt | AC | 71 ms | 6076 KiB |
| random_16.txt | AC | 70 ms | 6492 KiB |
| random_17.txt | AC | 70 ms | 8152 KiB |
| random_18.txt | AC | 70 ms | 6988 KiB |
| random_19.txt | AC | 71 ms | 7932 KiB |
| random_20.txt | AC | 70 ms | 7488 KiB |
| random_21.txt | AC | 71 ms | 8468 KiB |
| random_22.txt | AC | 70 ms | 6728 KiB |
| random_23.txt | AC | 71 ms | 8024 KiB |
| random_24.txt | AC | 71 ms | 6760 KiB |
| random_25.txt | AC | 72 ms | 9096 KiB |
| random_26.txt | AC | 70 ms | 6160 KiB |
| random_27.txt | AC | 71 ms | 7572 KiB |
| random_28.txt | AC | 70 ms | 6804 KiB |
| random_29.txt | AC | 71 ms | 6496 KiB |