#include <iostream> #include <vector> #include <map> using namespace std; typedef long long ll; int main() { int n; cin >> n; map<ll, ll> pos; for(ll i=0; i<n; i++){ ll x; cin>> x; pos[x] = i; } ll ans = 2*(n) + 1; ll cnt = 1; ll l = pos[n]; ll r = pos[n]; // Przypodek dla 2 if(n>1){ ll cur_val = n-1; ll cur_pos = pos[cur_val]; l = min(l, cur_pos); r = max(r, cur_pos); if(r-l==1){ cnt++; } ll x = 0; ll inside = 2; for(int i=2; i<n; i++){ if(i%2==0){ x++; }else{ cur_val--; cur_pos = pos[cur_val]; l = min(l, cur_pos); r = max(r, cur_pos); inside++; } //cout << cur_val << " " << x << "\n"; ll between = r-l-1 - (inside-2); ll to_spend = x - between; //cout << l << " " << r << " " << to_spend << endl; if(to_spend>0){ ll left = min(l, to_spend); ll right = min(n-1-r, to_spend); ll opt = left+right - to_spend + 1; if(opt>0){ cnt += opt; } }else if(to_spend==0){ cnt++; } //cout << left << " " << right << "\n"; } } cout << ans << " " << cnt; }
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <iostream> #include <vector> #include <map> using namespace std; typedef long long ll; int main() { int n; cin >> n; map<ll, ll> pos; for(ll i=0; i<n; i++){ ll x; cin>> x; pos[x] = i; } ll ans = 2*(n) + 1; ll cnt = 1; ll l = pos[n]; ll r = pos[n]; // Przypodek dla 2 if(n>1){ ll cur_val = n-1; ll cur_pos = pos[cur_val]; l = min(l, cur_pos); r = max(r, cur_pos); if(r-l==1){ cnt++; } ll x = 0; ll inside = 2; for(int i=2; i<n; i++){ if(i%2==0){ x++; }else{ cur_val--; cur_pos = pos[cur_val]; l = min(l, cur_pos); r = max(r, cur_pos); inside++; } //cout << cur_val << " " << x << "\n"; ll between = r-l-1 - (inside-2); ll to_spend = x - between; //cout << l << " " << r << " " << to_spend << endl; if(to_spend>0){ ll left = min(l, to_spend); ll right = min(n-1-r, to_spend); ll opt = left+right - to_spend + 1; if(opt>0){ cnt += opt; } }else if(to_spend==0){ cnt++; } //cout << left << " " << right << "\n"; } } cout << ans << " " << cnt; } |