#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 7; typedef long long ll; int t[MAX]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1, a; i <= n; i++){ cin >> a; t[a] = i; } int poc = t[n], kon = t[n]; ll ans = 1; for(int i = n-1, x; i*2 > n; i--){ if(t[i] > kon) kon = t[i]; if(t[i] < poc) poc = t[i]; if(kon - poc < 2*(n-i)){ x = 2*(n-i) + poc - kon-1; if(x+2 == i) ans++; else ans += min(min(poc, n - kon + 1), x+2); if(x == 0) ans++; else if(x > 0) ans += min(min(poc, n - kon + 1), x+1); } else if(kon - poc == 2*(n-i)) ans++; } if(n % 2 == 0) ans++; cout << 2*n + 1 << " " << ans; 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 | #include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 7; typedef long long ll; int t[MAX]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for(int i = 1, a; i <= n; i++){ cin >> a; t[a] = i; } int poc = t[n], kon = t[n]; ll ans = 1; for(int i = n-1, x; i*2 > n; i--){ if(t[i] > kon) kon = t[i]; if(t[i] < poc) poc = t[i]; if(kon - poc < 2*(n-i)){ x = 2*(n-i) + poc - kon-1; if(x+2 == i) ans++; else ans += min(min(poc, n - kon + 1), x+2); if(x == 0) ans++; else if(x > 0) ans += min(min(poc, n - kon + 1), x+1); } else if(kon - poc == 2*(n-i)) ans++; } if(n % 2 == 0) ans++; cout << 2*n + 1 << " " << ans; return 0; } |