#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector <ll> v(n + 1); for (int i = 0; i < n; i++) { int a; cin >> a; v[a] = i; } ll l = v[n], r = v[n]; ll res = 1; for (int i = n-1; i > 0; i--) { l = min(l, v[i]); r = max(r, v[i]); ll x = v[i-1]; ll dlugosc = (r - l + 1); ll duze = n - i + 1; ll male = dlugosc - duze; ll powinnobyc = dlugosc / 2 + 1; // cout << "duze:" << duze << " male:" << male << " dlugosc:" << dlugosc << " powinnobyc:" << powinnobyc << "\n"; if (powinnobyc > duze || (l < x && x < r)) continue; ll mogamale = duze - 1; ll dp = mogamale - male; ll pp = min(n - r - 1, dp); if (x > r) pp = min(x - r - 1, dp); ll pl = min(l, dp); if (x < l) pl = min(l - x - 1, dp); dp = min(dp, pl + pp); // cout << "l:" << l << " r:" << r << " dp:" << dp << " pl:" << pl << " pp:" << pp << " "; // cout << "deltares:" << (pl+1) * (pp+1) - (pp+pl-dp)*(pp+pl-dp+1)/ 2 << "\n"; res += (pl+1) * (pp+1) - (pp+pl-dp)*(pp+pl-dp+1)/ 2; } cout << 2 * n + 1 << " "; cout << res << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector <ll> v(n + 1); for (int i = 0; i < n; i++) { int a; cin >> a; v[a] = i; } ll l = v[n], r = v[n]; ll res = 1; for (int i = n-1; i > 0; i--) { l = min(l, v[i]); r = max(r, v[i]); ll x = v[i-1]; ll dlugosc = (r - l + 1); ll duze = n - i + 1; ll male = dlugosc - duze; ll powinnobyc = dlugosc / 2 + 1; // cout << "duze:" << duze << " male:" << male << " dlugosc:" << dlugosc << " powinnobyc:" << powinnobyc << "\n"; if (powinnobyc > duze || (l < x && x < r)) continue; ll mogamale = duze - 1; ll dp = mogamale - male; ll pp = min(n - r - 1, dp); if (x > r) pp = min(x - r - 1, dp); ll pl = min(l, dp); if (x < l) pl = min(l - x - 1, dp); dp = min(dp, pl + pp); // cout << "l:" << l << " r:" << r << " dp:" << dp << " pl:" << pl << " pp:" << pp << " "; // cout << "deltares:" << (pl+1) * (pp+1) - (pp+pl-dp)*(pp+pl-dp+1)/ 2 << "\n"; res += (pl+1) * (pp+1) - (pp+pl-dp)*(pp+pl-dp+1)/ 2; } cout << 2 * n + 1 << " "; cout << res << "\n"; } |