#include <bits/stdc++.h> using namespace std; #define ll long long vector<ll> V; vector<ll> M; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int nums; cin >> nums; V.resize(nums); M.resize(nums); for (size_t i = 0; i < nums; i++) { cin >> V[i]; M[V[i]] = i; } int lookfor1 = nums; int lookfor2 = nums; int najwId = M[nums]; ll res = 0; int k1 = INT_MIN; int k2 = INT_MAX; for (int range = 1; range <= nums; range++) { int findmin = M[lookfor1]; int findmax = M[lookfor2]; k1 = max(k1, max(findmin, findmax)); k2 = min(k2, min(findmin, findmax)); int left_size = range - (max(max(k1, k2), najwId) - min(min(k1, k2), najwId)) - 1; if (left_size >= 0) { int L = max(0, min(min(k1, k2), najwId) - left_size); int R = min(nums - 1, max(max(k1, k2), najwId) + left_size); res += R - L - range + 2; } lookfor1--; swap(lookfor1, lookfor2); } cout << 2 * nums + 1 << " " << res; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long vector<ll> V; vector<ll> M; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int nums; cin >> nums; V.resize(nums); M.resize(nums); for (size_t i = 0; i < nums; i++) { cin >> V[i]; M[V[i]] = i; } int lookfor1 = nums; int lookfor2 = nums; int najwId = M[nums]; ll res = 0; int k1 = INT_MIN; int k2 = INT_MAX; for (int range = 1; range <= nums; range++) { int findmin = M[lookfor1]; int findmax = M[lookfor2]; k1 = max(k1, max(findmin, findmax)); k2 = min(k2, min(findmin, findmax)); int left_size = range - (max(max(k1, k2), najwId) - min(min(k1, k2), najwId)) - 1; if (left_size >= 0) { int L = max(0, min(min(k1, k2), najwId) - left_size); int R = min(nums - 1, max(max(k1, k2), najwId) + left_size); res += R - L - range + 2; } lookfor1--; swap(lookfor1, lookfor2); } cout << 2 * nums + 1 << " " << res; } |