#include <cstdio> #include <algorithm> using namespace std; const int MAXN = 1000000; int a[MAXN+10]; int maxl[MAXN+10]; int maxr[MAXN+10]; int main() { int n, nodel; long long wynik = 0; scanf("%d", &n); maxl[0] = 0; maxr[n+1] = 0; for (int i = 1; i <= n; ++i) { scanf("%d", a+i); maxl[i] = max(maxl[i-1], a[i]); } for (int i = n; i >= 1; --i) { maxr[i] = max(maxr[i+1], a[i]); } int l = 0, r = n+1, ul, ur; for (int i = 0; i <= n; ++i) { nodel = n - (n-i+2)/2 + 1; while (maxl[l]<nodel) ++l; --l; while(maxr[r]<nodel) --r; ++r; ul = min(i, l); ur = max(n+1-i, r); if (ul + (n+1-ur) >= i) { wynik += ul + (n+1-ur) - i + 1; } } printf("%d %lld\n", 2*n+1, wynik); return 0; }
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 | #include <cstdio> #include <algorithm> using namespace std; const int MAXN = 1000000; int a[MAXN+10]; int maxl[MAXN+10]; int maxr[MAXN+10]; int main() { int n, nodel; long long wynik = 0; scanf("%d", &n); maxl[0] = 0; maxr[n+1] = 0; for (int i = 1; i <= n; ++i) { scanf("%d", a+i); maxl[i] = max(maxl[i-1], a[i]); } for (int i = n; i >= 1; --i) { maxr[i] = max(maxr[i+1], a[i]); } int l = 0, r = n+1, ul, ur; for (int i = 0; i <= n; ++i) { nodel = n - (n-i+2)/2 + 1; while (maxl[l]<nodel) ++l; --l; while(maxr[r]<nodel) --r; ++r; ul = min(i, l); ur = max(n+1-i, r); if (ul + (n+1-ur) >= i) { wynik += ul + (n+1-ur) - i + 1; } } printf("%d %lld\n", 2*n+1, wynik); return 0; } |