#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; constexpr int M = 1e6+7; int pos[M]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long res = 1; int n, a, fi, lst, best, xd1, xd2, xd3, xd4; cin >> n; for(int i=1; i<=n; i++){ cin >> a; pos[a] = i; } fi = lst = pos[n]; best = n; for(int i=2; i<=n; i++){ if(!(i&1)) best--; fi = min(fi, pos[best]); lst = max(lst, pos[best]); if(lst - fi + 1 > i) continue; xd1 = fi - 1; xd2 = n - lst; xd3 = n - i; xd4 = i - (lst - fi + 1); res += min({xd1, xd2, xd3, xd4}) + 1; //cout << "i=" << i << " fi=" << fi << " lst=" << lst << " best=" << best << " xd1=" << xd1 << " xd2=" << xd2 << " xd3=" << xd3 << " xd4=" << xd4 << " res=" << res << '\n'; } 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 | #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; constexpr int M = 1e6+7; int pos[M]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long res = 1; int n, a, fi, lst, best, xd1, xd2, xd3, xd4; cin >> n; for(int i=1; i<=n; i++){ cin >> a; pos[a] = i; } fi = lst = pos[n]; best = n; for(int i=2; i<=n; i++){ if(!(i&1)) best--; fi = min(fi, pos[best]); lst = max(lst, pos[best]); if(lst - fi + 1 > i) continue; xd1 = fi - 1; xd2 = n - lst; xd3 = n - i; xd4 = i - (lst - fi + 1); res += min({xd1, xd2, xd3, xd4}) + 1; //cout << "i=" << i << " fi=" << fi << " lst=" << lst << " best=" << best << " xd1=" << xd1 << " xd2=" << xd2 << " xd3=" << xd3 << " xd4=" << xd4 << " res=" << res << '\n'; } cout << 2*n+1 << ' ' << res << '\n'; return 0; } |