#include <iostream>
constexpr int maxN = 1e6;
constexpr int INF = 1e9 + 7;
int n;
int a;
int index[maxN + 1];
int _min;
int lth;
int l, r;
int ile;
int lewo, prawo;
long long ans_max, ans_cnt;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> n;
_min = n;
ans_max = 1 + (2 * n);
for(int i=1; i<=n; i++) {
std::cin >> a;
index[a] = i;
}
l = INF;
r = 0;
for(int i=1; i<=n; i++) {
l = std::min(l, index[_min]);
r = std::max(r, index[_min]);
lth = r - l + 1;
if(lth == i) {
ans_cnt++;
}
else if(lth < i) {
ile = i - lth;
lewo = l - 1;
prawo = n - r;
if(lewo > 0 && prawo > 0) {
if(lewo >= ile && prawo >= ile) {
ans_cnt += ile + 1;
}
else if(lewo >= ile || prawo >= ile) {
ans_cnt += std::min(lewo, prawo) + 1;
}
else {
ans_cnt += lewo + prawo - ile + 1;
}
}
else if(lewo + prawo > 0) {
ans_cnt++;
}
}
if(i & 1) {
_min--;
}
}
std::cout << ans_max << ' ' << ans_cnt;
}
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 66 67 68 69 70 71 72 73 | #include <iostream> constexpr int maxN = 1e6; constexpr int INF = 1e9 + 7; int n; int a; int index[maxN + 1]; int _min; int lth; int l, r; int ile; int lewo, prawo; long long ans_max, ans_cnt; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); std::cin >> n; _min = n; ans_max = 1 + (2 * n); for(int i=1; i<=n; i++) { std::cin >> a; index[a] = i; } l = INF; r = 0; for(int i=1; i<=n; i++) { l = std::min(l, index[_min]); r = std::max(r, index[_min]); lth = r - l + 1; if(lth == i) { ans_cnt++; } else if(lth < i) { ile = i - lth; lewo = l - 1; prawo = n - r; if(lewo > 0 && prawo > 0) { if(lewo >= ile && prawo >= ile) { ans_cnt += ile + 1; } else if(lewo >= ile || prawo >= ile) { ans_cnt += std::min(lewo, prawo) + 1; } else { ans_cnt += lewo + prawo - ile + 1; } } else if(lewo + prawo > 0) { ans_cnt++; } } if(i & 1) { _min--; } } std::cout << ans_max << ' ' << ans_cnt; } |
English