#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; int tab[N]; int rev[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &tab[i]); rev[tab[i]] = i; } long long res = 0; int mn = n + 1; int mx = 0; for (int i = n; i >= 0; i--) { int dl = n - i; mn = min(mn, rev[i + dl / 2]); mx = max(mx, rev[i + dl / 2]); int up = min(mn - 1, i - 1); int dw = max((i - 1) - (n - mx), 0); // cout << "siema " << i << " " << up << " " << dw << endl; res += max(0, up - dw + 1); } printf("%d %lld\n", n + (n + 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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n; int tab[N]; int rev[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &tab[i]); rev[tab[i]] = i; } long long res = 0; int mn = n + 1; int mx = 0; for (int i = n; i >= 0; i--) { int dl = n - i; mn = min(mn, rev[i + dl / 2]); mx = max(mx, rev[i + dl / 2]); int up = min(mn - 1, i - 1); int dw = max((i - 1) - (n - mx), 0); // cout << "siema " << i << " " << up << " " << dw << endl; res += max(0, up - dw + 1); } printf("%d %lld\n", n + (n + 1), res); } |