#include <cstdio> #include <algorithm> using namespace std; bool DEBUG = false; struct Range { int start; int end; Range(int start, int end) { this->start = start; this->end = end; } void extend(int p) { if (p < this->start) { this->start = p; } if (p > this->end) { this->end = p; } } void print() { } int size() { return this->end - this->start + 1; } int ways_to_extend_by(int k, int max) { if (k == 0) { return 1; } if (k < 0) { return 0; } int space_left = min(k, this->start); int space_right = min(k, max - this->end - 1); int total_left = space_left + space_right; if (total_left < k) { return 0; } return total_left - k + 1; } }; int main(int argc, char const *argv[]) { int n; int* a; int* positions; scanf("%d", &n); printf("%d ", n*2 + 1); a = new int[n + 100]; positions = new int[n + 100]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); positions[a[i]] = i; } int position = positions[n]; Range current_range = Range(position, position); long long result = 0; for (int j=n; j > 0; j--) { current_range.extend(positions[j]); int size = current_range.size(); int max_size = (n-j+1)*2-1; result += current_range.ways_to_extend_by(max_size - size, n); result += current_range.ways_to_extend_by(max_size - size - 1, n); } printf("%lld\n", result); 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #include <cstdio> #include <algorithm> using namespace std; bool DEBUG = false; struct Range { int start; int end; Range(int start, int end) { this->start = start; this->end = end; } void extend(int p) { if (p < this->start) { this->start = p; } if (p > this->end) { this->end = p; } } void print() { } int size() { return this->end - this->start + 1; } int ways_to_extend_by(int k, int max) { if (k == 0) { return 1; } if (k < 0) { return 0; } int space_left = min(k, this->start); int space_right = min(k, max - this->end - 1); int total_left = space_left + space_right; if (total_left < k) { return 0; } return total_left - k + 1; } }; int main(int argc, char const *argv[]) { int n; int* a; int* positions; scanf("%d", &n); printf("%d ", n*2 + 1); a = new int[n + 100]; positions = new int[n + 100]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); positions[a[i]] = i; } int position = positions[n]; Range current_range = Range(position, position); long long result = 0; for (int j=n; j > 0; j--) { current_range.extend(positions[j]); int size = current_range.size(); int max_size = (n-j+1)*2-1; result += current_range.ways_to_extend_by(max_size - size, n); result += current_range.ways_to_extend_by(max_size - size - 1, n); } printf("%lld\n", result); return 0; } |