#include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n, t, pos[1000006], a, minn, maxx, dl1, dl2, l, r; ll res; int main() { qio; cin >> n; for (int i = 1; i <= n; i++) { cin >> a; pos[a] = i; } for (int i = 1; i <= n / 2 + 1; i++) { if (i == 1) { minn = pos[n - i + 1]; maxx = pos[n - i + 1]; } else { minn = min(minn, pos[n - i + 1]); maxx = max(maxx, pos[n - i + 1]); } dl1 = i * 2 - 2; dl2 = i * 2 - 1; if (dl1 > 0) { l = max(1, maxx - dl1 + 1); r = min(n, minn + dl1 - 1); res += max(0, r - l + 1 - dl1 + 1); } if (dl2 <= n) { l = max(1, maxx - dl2 + 1); r = min(n, minn + dl2 - 1); res += max(0, r - l + 1 - dl2 + 1); } //cout << i << " " << minn << " " << maxx << endl; } cout << n * 2 + 1 << " " << res << 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 | #include <bits/stdc++.h> #define qio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define debug(x) cerr<<#x<<" "<<x<<endl #define ll long long #define st first #define nd second using namespace std; int n, t, pos[1000006], a, minn, maxx, dl1, dl2, l, r; ll res; int main() { qio; cin >> n; for (int i = 1; i <= n; i++) { cin >> a; pos[a] = i; } for (int i = 1; i <= n / 2 + 1; i++) { if (i == 1) { minn = pos[n - i + 1]; maxx = pos[n - i + 1]; } else { minn = min(minn, pos[n - i + 1]); maxx = max(maxx, pos[n - i + 1]); } dl1 = i * 2 - 2; dl2 = i * 2 - 1; if (dl1 > 0) { l = max(1, maxx - dl1 + 1); r = min(n, minn + dl1 - 1); res += max(0, r - l + 1 - dl1 + 1); } if (dl2 <= n) { l = max(1, maxx - dl2 + 1); r = min(n, minn + dl2 - 1); res += max(0, r - l + 1 - dl2 + 1); } //cout << i << " " << minn << " " << maxx << endl; } cout << n * 2 + 1 << " " << res << endl; } |