//Author: Piotr Zielinski #include<bits/stdc++.h> using namespace std; typedef long long ll; int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> p(n); vector<int> pos(n + 5); for(int i = 0;i < n;++i) { cin >> p[i]; pos[p[i]] = i; } ll res = 1; int l = pos[n], r = pos[n]; for(int i = 1, siz = 2;i < n;++i, siz += 2) { l = min(l, pos[n - i]); r = max(r, pos[n - i]); int d = r - l + 1; // debug(n - i, siz, d); if(d <= siz + 1) { int newL = r - siz + 1, newR = r; if(newL < 0) { newR -= newL; newL = 0; } res += max(0, min(n - newR, l - newL + 1)); newR = r; newL = r - siz; if(newL < 0) { newR -= newL; newL = 0; } res += max(0, min(n - newR, l - newL + 1)); } } cout << 2 * n + 1 << " " << res << "\n"; 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 | //Author: Piotr Zielinski #include<bits/stdc++.h> using namespace std; typedef long long ll; int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> p(n); vector<int> pos(n + 5); for(int i = 0;i < n;++i) { cin >> p[i]; pos[p[i]] = i; } ll res = 1; int l = pos[n], r = pos[n]; for(int i = 1, siz = 2;i < n;++i, siz += 2) { l = min(l, pos[n - i]); r = max(r, pos[n - i]); int d = r - l + 1; // debug(n - i, siz, d); if(d <= siz + 1) { int newL = r - siz + 1, newR = r; if(newL < 0) { newR -= newL; newL = 0; } res += max(0, min(n - newR, l - newL + 1)); newR = r; newL = r - siz; if(newL < 0) { newR -= newL; newL = 0; } res += max(0, min(n - newR, l - newL + 1)); } } cout << 2 * n + 1 << " " << res << "\n"; return 0; } |