#include <iostream> #include <vector> #include <algorithm> int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> v(n); std::vector<int> r(n); std::vector<int> ma(n); std::vector<int> mi(n); for (int i = 0; i < n; ++i) { std::cin >> v[i]; v[i]--; r[v[i]] = i; if (v[i] == n - 1) { ma[n - 1] = i; mi[n - 1] = i; } } for (int i = n - 2; i >= 0; --i) { ma[i] = std::max(ma[i + 1], r[i]); mi[i] = std::min(mi[i + 1], r[i]); } long long res = 0; for (int i = 1; i <= n; ++i) { int num = i / 2 + 1; int a = mi[n - num]; int b = ma[n - num]; int rem = i - (b - a + 1); if (rem < 0) continue; b = n - b - 1; int x = std::max(0, rem - b); int y = std::min(rem, a); res += std::max(0, y - x + 1); } std::cout << 2 * n + 1 << ' ' << res; 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 | #include <iostream> #include <vector> #include <algorithm> int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> v(n); std::vector<int> r(n); std::vector<int> ma(n); std::vector<int> mi(n); for (int i = 0; i < n; ++i) { std::cin >> v[i]; v[i]--; r[v[i]] = i; if (v[i] == n - 1) { ma[n - 1] = i; mi[n - 1] = i; } } for (int i = n - 2; i >= 0; --i) { ma[i] = std::max(ma[i + 1], r[i]); mi[i] = std::min(mi[i + 1], r[i]); } long long res = 0; for (int i = 1; i <= n; ++i) { int num = i / 2 + 1; int a = mi[n - num]; int b = ma[n - num]; int rem = i - (b - a + 1); if (rem < 0) continue; b = n - b - 1; int x = std::max(0, rem - b); int y = std::min(rem, a); res += std::max(0, y - x + 1); } std::cout << 2 * n + 1 << ' ' << res; return 0; } |