#include <bits/stdc++.h> #include <unordered_set> using namespace std; const int N = int(1e6) + 99; int n, x, k, p, q, diff; int a[N]; int b[N]; long long result; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i]] = i; } int median = n + 1; int l = b[n], r = b[n]; result = 0; for (int i = 1; i <= n; i++) { int good = i / 2 + 1; int minimal = n - good + 1; int random = i - good; x = b[minimal]; if (x <= l) { l = x; } else if (r <= x) { r = x; } diff = r-l+1; // cout << "DIFF: " << diff << endl; if (diff > i) { continue; } diff = i - diff; // cout << "DIFF: " << diff << endl; if (l - diff < 1) { p = l - 1; } else { p = diff; } if (r + diff > n) { q = n - r; } else { q = diff; } int freedom = p + q + 1 - diff; result += freedom; // cout << "p:" << p << ", q:" << q << endl; // cout << "i:" << i << ", f:" << freedom << endl; } cout << n * 2 + 1 << " " << result << endl; }
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 | #include <bits/stdc++.h> #include <unordered_set> using namespace std; const int N = int(1e6) + 99; int n, x, k, p, q, diff; int a[N]; int b[N]; long long result; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i]] = i; } int median = n + 1; int l = b[n], r = b[n]; result = 0; for (int i = 1; i <= n; i++) { int good = i / 2 + 1; int minimal = n - good + 1; int random = i - good; x = b[minimal]; if (x <= l) { l = x; } else if (r <= x) { r = x; } diff = r-l+1; // cout << "DIFF: " << diff << endl; if (diff > i) { continue; } diff = i - diff; // cout << "DIFF: " << diff << endl; if (l - diff < 1) { p = l - 1; } else { p = diff; } if (r + diff > n) { q = n - r; } else { q = diff; } int freedom = p + q + 1 - diff; result += freedom; // cout << "p:" << p << ", q:" << q << endl; // cout << "i:" << i << ", f:" << freedom << endl; } cout << n * 2 + 1 << " " << result << endl; } |