#include <bits/stdc++.h> using namespace std; const int N = 1'000'007; int n; int in[N]; int place[N]; int suf[N][2]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); place[in[i]] = i; } suf[n + 1][0] = n; suf[n + 1][1] = 1; for (int i = n; i >= 1; --i) { suf[i][0] = min(suf[i + 1][0], place[i]); suf[i][1] = max(suf[i + 1][1], place[i]); } long long ans = 0; for (int i = n; i >= 1; --i) { int len = (i + 2) / 2; int from = suf[n - len + 1][0], to = suf[n - len + 1][1]; if (to - from + 1 > i) { continue; } int can = min(from, n + 1 - to); can = min(can, i - (to - from)); can = min(can, n - i + 1); ans += can; } printf("%d %lld\n", 2 * n + 1, ans); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include <bits/stdc++.h> using namespace std; const int N = 1'000'007; int n; int in[N]; int place[N]; int suf[N][2]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); place[in[i]] = i; } suf[n + 1][0] = n; suf[n + 1][1] = 1; for (int i = n; i >= 1; --i) { suf[i][0] = min(suf[i + 1][0], place[i]); suf[i][1] = max(suf[i + 1][1], place[i]); } long long ans = 0; for (int i = n; i >= 1; --i) { int len = (i + 2) / 2; int from = suf[n - len + 1][0], to = suf[n - len + 1][1]; if (to - from + 1 > i) { continue; } int can = min(from, n + 1 - to); can = min(can, i - (to - from)); can = min(can, n - i + 1); ans += can; } printf("%d %lld\n", 2 * n + 1, ans); return 0; } |