#include <bits/stdc++.h> using namespace std; #define boost \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) long long solve(const vector<int> &a) { const int n = a.size(); vector<int> pos(n+1); for (int i = 0; i < n; ++i) { pos[a[i]] = i; } pos[0] = -1; int l = pos[n], r = pos[n]; long long res = 0; for (int segment_len = 1; segment_len <= n; ++segment_len) { int new_pos = pos[n - segment_len + 1], next_pos = pos[n - segment_len]; l = min(l, new_pos); r = max(r, new_pos); if (next_pos >= l && next_pos <= r) continue; int max_size = 2*segment_len - 1, current_len = r - l + 1; long long left_extend = next_pos < l ? l - next_pos : l + 1; long long right_extend = next_pos > r ? next_pos - r : n - r; if (max_size >= current_len) { long long max_extend = max_size - current_len + 1; left_extend = min(left_extend, max_extend); right_extend = min(right_extend, max_extend); int eq_steps = max(0ll, min(left_extend, max_extend - right_extend)); int rem_steps = left_extend - eq_steps; long long add1 = right_extend * eq_steps + ((2*right_extend - rem_steps + 1) * rem_steps) / 2; res += add1; } } return res; } int main() { boost; int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; cout << 2*n + 1 << " " << solve(a) << '\n'; return 0; } /* 5 1 4 3 5 2 6 1 2 4 5 6 3 0 4 0 5 1 4 1 5 2 4 2 5 3 4 3 5 4 4 6 1 2 3 5 4 6 6 1 2 3 5 6 4 6 1 2 5 6 3 4 */
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <bits/stdc++.h> using namespace std; #define boost \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) long long solve(const vector<int> &a) { const int n = a.size(); vector<int> pos(n+1); for (int i = 0; i < n; ++i) { pos[a[i]] = i; } pos[0] = -1; int l = pos[n], r = pos[n]; long long res = 0; for (int segment_len = 1; segment_len <= n; ++segment_len) { int new_pos = pos[n - segment_len + 1], next_pos = pos[n - segment_len]; l = min(l, new_pos); r = max(r, new_pos); if (next_pos >= l && next_pos <= r) continue; int max_size = 2*segment_len - 1, current_len = r - l + 1; long long left_extend = next_pos < l ? l - next_pos : l + 1; long long right_extend = next_pos > r ? next_pos - r : n - r; if (max_size >= current_len) { long long max_extend = max_size - current_len + 1; left_extend = min(left_extend, max_extend); right_extend = min(right_extend, max_extend); int eq_steps = max(0ll, min(left_extend, max_extend - right_extend)); int rem_steps = left_extend - eq_steps; long long add1 = right_extend * eq_steps + ((2*right_extend - rem_steps + 1) * rem_steps) / 2; res += add1; } } return res; } int main() { boost; int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; cout << 2*n + 1 << " " << solve(a) << '\n'; return 0; } /* 5 1 4 3 5 2 6 1 2 4 5 6 3 0 4 0 5 1 4 1 5 2 4 2 5 3 4 3 5 4 4 6 1 2 3 5 4 6 6 1 2 3 5 6 4 6 1 2 5 6 3 4 */ |