#include <cstdio> #include <algorithm> int p[1000005]; int l[1000005]; int u[1000005]; int main() { int n, x; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x); p[x] = i+1; } l[n] = p[n]; u[n] = p[n]; for (int i = n - 1; i > 0; i--) { l[i] = p[i] < l[i+1] ? p[i] : l[i+1]; u[i] = p[i] > u[i+1] ? p[i] : u[i+1]; } long long int ans = 0; for (int k = 1; k <= n; k++) { int i = l[n-k/2]; int j = u[n-k/2]; int d = k-(j-i+1); if (d < 0) continue; if (j + d > n) d = n - j; if (j < k) d += j - k; ans += d + 1; } 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 | #include <cstdio> #include <algorithm> int p[1000005]; int l[1000005]; int u[1000005]; int main() { int n, x; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &x); p[x] = i+1; } l[n] = p[n]; u[n] = p[n]; for (int i = n - 1; i > 0; i--) { l[i] = p[i] < l[i+1] ? p[i] : l[i+1]; u[i] = p[i] > u[i+1] ? p[i] : u[i+1]; } long long int ans = 0; for (int k = 1; k <= n; k++) { int i = l[n-k/2]; int j = u[n-k/2]; int d = k-(j-i+1); if (d < 0) continue; if (j + d > n) d = n - j; if (j < k) d += j - k; ans += d + 1; } printf("%d %lld\n", 2*n+1, ans); return 0; } |