#include <bits/stdc++.h> using namespace std; const int t = 1e6 + 9, inf = 1e9; int tab[t], pref[t], suf[t]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i = 0; i < n; i ++) cin >> tab[i]; pref[0] = tab[0]; for(int i = 1; i < n; i ++) pref[i] = max(tab[i], pref[i - 1]); suf[n - 1] = tab[n - 1]; for(int i = n - 2; i >= 0; i --) suf[i] = max(tab[i], suf[i + 1]); long long wynik = 0; for(int i = 0; i < n; i ++) { int lo = 0, hi = n - 1, mid, a = -1, b = n; while(lo <= hi) { mid = (lo + hi) / 2; if(pref[mid] < (n + 1 + i) / 2) { a = mid; lo = mid + 1; } else hi = mid - 1; } lo = 0; hi = n - 1; while(lo <= hi) { mid = (lo + hi) / 2; if(suf[mid] < (n + 1 + i) / 2) { b = mid; hi = mid - 1; } else lo = mid + 1; } a ++; b = n - b; int c = min(a, b); b = max(a, b); a = c; if(a + b >= i) { int x = max(0, i - b); int y = min(a, i); //cout << a << ' ' << b << '\n'; //cout << x << ' ' << y << ' ' << y - x + 1 << '\n'; wynik += (y - x + 1); } } cout << n * 2 + 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 42 43 44 45 46 47 48 49 50 51 52 | #include <bits/stdc++.h> using namespace std; const int t = 1e6 + 9, inf = 1e9; int tab[t], pref[t], suf[t]; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for(int i = 0; i < n; i ++) cin >> tab[i]; pref[0] = tab[0]; for(int i = 1; i < n; i ++) pref[i] = max(tab[i], pref[i - 1]); suf[n - 1] = tab[n - 1]; for(int i = n - 2; i >= 0; i --) suf[i] = max(tab[i], suf[i + 1]); long long wynik = 0; for(int i = 0; i < n; i ++) { int lo = 0, hi = n - 1, mid, a = -1, b = n; while(lo <= hi) { mid = (lo + hi) / 2; if(pref[mid] < (n + 1 + i) / 2) { a = mid; lo = mid + 1; } else hi = mid - 1; } lo = 0; hi = n - 1; while(lo <= hi) { mid = (lo + hi) / 2; if(suf[mid] < (n + 1 + i) / 2) { b = mid; hi = mid - 1; } else lo = mid + 1; } a ++; b = n - b; int c = min(a, b); b = max(a, b); a = c; if(a + b >= i) { int x = max(0, i - b); int y = min(a, i); //cout << a << ' ' << b << '\n'; //cout << x << ' ' << y << ' ' << y - x + 1 << '\n'; wynik += (y - x + 1); } } cout << n * 2 + 1 << ' ' << wynik; return 0; } |