#include <iostream> #include <vector> #include <algorithm> using namespace std; void solve(){ int n; cin >> n; vector<int> pos(n+1, 0); for(int i = 1; i <= n; i++) { int a; cin >> a; pos[a] = i; } long long count = 1; int beg = pos[n], end = pos[n]; int next_val = n-1; int good_size = 1; while(next_val > 0) { good_size += 2; if(pos[next_val] > end) { end = pos[next_val]; } else if(pos[next_val] < beg) { beg = pos[next_val]; } if(pos[next_val-1] >= beg && pos[next_val-1] <= end) { next_val--; continue; } else { int free_numbers = good_size - (end - beg + 1); if(free_numbers >= 0) { int window_front = max(1, beg - free_numbers); if(pos[next_val-1] < beg) window_front = max(window_front, pos[next_val-1] + 1); int window_back = min(n, end + free_numbers); if(pos[next_val-1] > end) window_back = min(window_back, pos[next_val-1] - 1); int full_size = min(free_numbers - (window_back - end) + 1, beg - window_front + 1); count += (long long) full_size * (window_back - end + 1); int part_end = beg - full_size; if(part_end >= window_front) { count += (long long) ((window_back - end) + (free_numbers - (beg - window_front) + 1)) * (part_end - window_front + 1) / 2; } } next_val--; } } cout << (2 * n) + 1 << " " << count << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #include <iostream> #include <vector> #include <algorithm> using namespace std; void solve(){ int n; cin >> n; vector<int> pos(n+1, 0); for(int i = 1; i <= n; i++) { int a; cin >> a; pos[a] = i; } long long count = 1; int beg = pos[n], end = pos[n]; int next_val = n-1; int good_size = 1; while(next_val > 0) { good_size += 2; if(pos[next_val] > end) { end = pos[next_val]; } else if(pos[next_val] < beg) { beg = pos[next_val]; } if(pos[next_val-1] >= beg && pos[next_val-1] <= end) { next_val--; continue; } else { int free_numbers = good_size - (end - beg + 1); if(free_numbers >= 0) { int window_front = max(1, beg - free_numbers); if(pos[next_val-1] < beg) window_front = max(window_front, pos[next_val-1] + 1); int window_back = min(n, end + free_numbers); if(pos[next_val-1] > end) window_back = min(window_back, pos[next_val-1] - 1); int full_size = min(free_numbers - (window_back - end) + 1, beg - window_front + 1); count += (long long) full_size * (window_back - end + 1); int part_end = beg - full_size; if(part_end >= window_front) { count += (long long) ((window_back - end) + (free_numbers - (beg - window_front) + 1)) * (part_end - window_front + 1) / 2; } } next_val--; } } cout << (2 * n) + 1 << " " << count << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; } |