#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> A(n+1), Pos(n+1); for (int i = 1; i <= n; i++) { cin >> A[i]; Pos[A[i]] = i; } ll ans = 0; // odd int l = n+1, r = -1; for (int k = 0; 2*k+1 <= n; k++) { int v = n-k, p = Pos[v]; l = min(l, p); r = max(r, p); int min_l = max(r-(2*k), 1); ans += max(min(l, n-(2*k)) - min_l + 1, 0); } // even l = n+1, r = -1; for (int k = 1; 2*k <= n; k++) { int v1 = n-k+1, v2 = n-k, p1 = Pos[v1], p2 = Pos[v2]; l = min(l, min(p1, p2)); r = max(r, max(p1, p2)); int min_l = max(r-(2*k-1), 1); ans += max(min(l, n-(2*k-1)) - min_l + 1, 0); } cout << 2*n+1 << " " << ans <<endl; 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 | #include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector <int> A(n+1), Pos(n+1); for (int i = 1; i <= n; i++) { cin >> A[i]; Pos[A[i]] = i; } ll ans = 0; // odd int l = n+1, r = -1; for (int k = 0; 2*k+1 <= n; k++) { int v = n-k, p = Pos[v]; l = min(l, p); r = max(r, p); int min_l = max(r-(2*k), 1); ans += max(min(l, n-(2*k)) - min_l + 1, 0); } // even l = n+1, r = -1; for (int k = 1; 2*k <= n; k++) { int v1 = n-k+1, v2 = n-k, p1 = Pos[v1], p2 = Pos[v2]; l = min(l, min(p1, p2)); r = max(r, max(p1, p2)); int min_l = max(r-(2*k-1), 1); ans += max(min(l, n-(2*k-1)) - min_l + 1, 0); } cout << 2*n+1 << " " << ans <<endl; return 0; } |