#include <iostream> #include <vector> using namespace std; int singleCase(const vector<int>& rev, int l, int r, int n, int pos, int val, int howMany = 0) { int toLeft = n - val - howMany, toRight = toLeft; int inside = max(0, r - l + 1 - toLeft - 1 - howMany); if(inside == toRight) { return 1; } if(inside < toRight) { int left_behind = n - (r - l + 1); int toGet = toRight - inside; if(left_behind - toGet < 0) { return 0; } int valLeft = (l - toGet < 0 ? l : toGet); int valRight = (r + toGet >= n ? n - r - 1 : toGet); return valLeft + valRight - toGet + 1; } return 0; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> nums(n), rev(n + 1); for(int i = 0; i < n; ++i) { cin >> nums[i]; rev[nums[i]] = i; } int target = 2 * n + 1, l = rev[n], r = l; long long res = 1; for(int i = n - 1; i >= 1; --i) { int pos = rev[i]; if(pos < l) { l = pos; } if(pos > r) { r = pos; } res += singleCase(rev, l, r, n, pos, i) + singleCase(rev, l, r, n, pos, i, 1); } cout << 2 * n + 1 << " " << 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 45 46 47 48 49 50 51 52 | #include <iostream> #include <vector> using namespace std; int singleCase(const vector<int>& rev, int l, int r, int n, int pos, int val, int howMany = 0) { int toLeft = n - val - howMany, toRight = toLeft; int inside = max(0, r - l + 1 - toLeft - 1 - howMany); if(inside == toRight) { return 1; } if(inside < toRight) { int left_behind = n - (r - l + 1); int toGet = toRight - inside; if(left_behind - toGet < 0) { return 0; } int valLeft = (l - toGet < 0 ? l : toGet); int valRight = (r + toGet >= n ? n - r - 1 : toGet); return valLeft + valRight - toGet + 1; } return 0; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> nums(n), rev(n + 1); for(int i = 0; i < n; ++i) { cin >> nums[i]; rev[nums[i]] = i; } int target = 2 * n + 1, l = rev[n], r = l; long long res = 1; for(int i = n - 1; i >= 1; --i) { int pos = rev[i]; if(pos < l) { l = pos; } if(pos > r) { r = pos; } res += singleCase(rev, l, r, n, pos, i) + singleCase(rev, l, r, n, pos, i, 1); } cout << 2 * n + 1 << " " << res << "\n"; } |