#include <bits/stdc++.h> using namespace std; int n; int value[1000007]; int index_of[1000007]; bool found[1000007]; long long result; int next_target; int L, R; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { cin >> value[i]; index_of[value[i]] = i; } L = index_of[n]; R = index_of[n]; next_target = n; while (next_target) { if (index_of[next_target] < L) { L--; } else if (R < index_of[next_target]) { R++; } found[L] = true; found[R] = true; if (L <= index_of[next_target] && index_of[next_target] <= R) { while (L <= index_of[next_target] && index_of[next_target] <= R) { found[next_target] = true; next_target--; } } int leftovers = (n - next_target) * 2 - 1 - (R - L + 1); if (leftovers < 0) { continue; } if (index_of[next_target] < L) { result += (long long) min(leftovers + 1, n - R + 1); } else { result += (long long) min(leftovers + 1, L); } } cout << 2 * n + 1 << " " << result << "\n"; 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <bits/stdc++.h> using namespace std; int n; int value[1000007]; int index_of[1000007]; bool found[1000007]; long long result; int next_target; int L, R; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { cin >> value[i]; index_of[value[i]] = i; } L = index_of[n]; R = index_of[n]; next_target = n; while (next_target) { if (index_of[next_target] < L) { L--; } else if (R < index_of[next_target]) { R++; } found[L] = true; found[R] = true; if (L <= index_of[next_target] && index_of[next_target] <= R) { while (L <= index_of[next_target] && index_of[next_target] <= R) { found[next_target] = true; next_target--; } } int leftovers = (n - next_target) * 2 - 1 - (R - L + 1); if (leftovers < 0) { continue; } if (index_of[next_target] < L) { result += (long long) min(leftovers + 1, n - R + 1); } else { result += (long long) min(leftovers + 1, L); } } cout << 2 * n + 1 << " " << result << "\n"; return 0; } |