#include <iostream> #include <map> #include <vector> std::vector<int> get_number_postions(const std::vector<int> &v) { std::vector<int> result(v.size()); for (size_t i = 0; i < v.size(); ++i) { result[v[i] - 1] = i; } return result; } int check_one(int x, int n, const std::vector<int> &POSITIONS, const std::vector<int> &RANKING) { // DEBUG // fprintf(stderr, "x: %d, n: %d\n", x, n); int result = 0; const int x_pos = POSITIONS[x - 1]; const int start = std::max(x_pos - n + 1, 0); int larger = 0; int smaller = 0; for (int i = 0; i < n; ++i) { if (RANKING[start + i] == x) { continue; } RANKING[start + i] > x ? larger++ : smaller++; } // DEBUG // fprintf(stderr, "x: %d, pos: %d, start: %d, larger: %d, smaller: %d\n", // x, // x_pos, start, larger, smaller); if (larger == smaller) { result++; } for (int i = start + 1; i < std::min(x_pos + 1, (int)RANKING.size() - n + 1); ++i) { RANKING[i - 1] > x ? larger-- : smaller--; RANKING[i + n - 1] > x ? larger++ : smaller++; if (larger == smaller) { result++; } } // DEBUG // fprintf(stderr, "result: %d\n", result); return result; } int check_two(int a, int b, int n, const std::vector<int> &POSITIONS, const std::vector<int> &RANKING) { // DEBUG // fprintf(stderr, "a: %d, b: %d, n: %d\n", a, b, n); int result = 0; int a_pos = POSITIONS[a - 1]; int b_pos = POSITIONS[b - 1]; if (a_pos > b_pos) { std::swap(a_pos, b_pos); } if (b_pos - a_pos >= n) { return 0; } if ((a - 1) + (n - b) < b_pos - a_pos - 1) { return 0; } const int start = std::max(b_pos - n + 1, 0); int larger = 0; int smaller = 0; int middle = 0; // DEBUG // fprintf(stderr, // "a: %d, b: %d, a_pos: %d, b_pos: %d, start: %d, larger: %d, // smaller: " // "%d, middle: %d\n", // a, b, a_pos, b_pos, start, larger, smaller, middle); for (int i = 0; i < n; ++i) { if (RANKING[start + i] == a || RANKING[start + i] == b) { continue; } if (RANKING[start + i] < a) { smaller++; } else if (RANKING[start + i] > b) { larger++; } else { if (start + i > a_pos && start + i < b_pos) { return 0; } middle++; } // DEBUG // fprintf(stderr, // "a: %d, b: %d, a_pos: %d, b_pos: %d, i: %d, larger: %d, // smaller: " // "%d, middle: %d\n", // a, b, a_pos, b_pos, i, larger, smaller, middle); } if (larger == smaller && middle == 0) { result++; } for (int i = start + 1; i < std::min(a_pos + 1, (int)RANKING.size() - n + 1); ++i) { if (RANKING[i - 1] < a) { smaller--; } else if (RANKING[i - 1] > b) { larger--; } else { middle--; } if (RANKING[i + n - 1] < a) { smaller++; } else if (RANKING[i + n - 1] > b) { larger++; } else { middle++; } if (larger == smaller && middle == 0) { result++; } } // DEBUG // fprintf(stderr, "result: %d\n", result); return result; } int process(const int N, const int MAX, const std::vector<int> &POSITIONS, const std::vector<int> &RANKING) { int result = 1; for (int n = N - 1; n > 0; --n) { if ((MAX - n) % 2) { for (int b = N; b > (MAX - n) / 2; --b) { result += check_two(MAX - n - b, b, n, POSITIONS, RANKING); } } else { result += check_one((MAX - n) / 2, n, POSITIONS, RANKING); } } return result; } int main() { int N = 0; std::cin >> N; std::vector<int> v(N); for (int i = 0; i < N; ++i) { std::cin >> v[i]; } /////////////////// const int MAX = 2 * N + 1; const std::vector<int> POSITIONS = get_number_postions(v); // DEBUG // std::cerr << "max: " << MAX << std::endl; std::cout << MAX << " " << process(N, MAX, POSITIONS, v) << std::endl; 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | #include <iostream> #include <map> #include <vector> std::vector<int> get_number_postions(const std::vector<int> &v) { std::vector<int> result(v.size()); for (size_t i = 0; i < v.size(); ++i) { result[v[i] - 1] = i; } return result; } int check_one(int x, int n, const std::vector<int> &POSITIONS, const std::vector<int> &RANKING) { // DEBUG // fprintf(stderr, "x: %d, n: %d\n", x, n); int result = 0; const int x_pos = POSITIONS[x - 1]; const int start = std::max(x_pos - n + 1, 0); int larger = 0; int smaller = 0; for (int i = 0; i < n; ++i) { if (RANKING[start + i] == x) { continue; } RANKING[start + i] > x ? larger++ : smaller++; } // DEBUG // fprintf(stderr, "x: %d, pos: %d, start: %d, larger: %d, smaller: %d\n", // x, // x_pos, start, larger, smaller); if (larger == smaller) { result++; } for (int i = start + 1; i < std::min(x_pos + 1, (int)RANKING.size() - n + 1); ++i) { RANKING[i - 1] > x ? larger-- : smaller--; RANKING[i + n - 1] > x ? larger++ : smaller++; if (larger == smaller) { result++; } } // DEBUG // fprintf(stderr, "result: %d\n", result); return result; } int check_two(int a, int b, int n, const std::vector<int> &POSITIONS, const std::vector<int> &RANKING) { // DEBUG // fprintf(stderr, "a: %d, b: %d, n: %d\n", a, b, n); int result = 0; int a_pos = POSITIONS[a - 1]; int b_pos = POSITIONS[b - 1]; if (a_pos > b_pos) { std::swap(a_pos, b_pos); } if (b_pos - a_pos >= n) { return 0; } if ((a - 1) + (n - b) < b_pos - a_pos - 1) { return 0; } const int start = std::max(b_pos - n + 1, 0); int larger = 0; int smaller = 0; int middle = 0; // DEBUG // fprintf(stderr, // "a: %d, b: %d, a_pos: %d, b_pos: %d, start: %d, larger: %d, // smaller: " // "%d, middle: %d\n", // a, b, a_pos, b_pos, start, larger, smaller, middle); for (int i = 0; i < n; ++i) { if (RANKING[start + i] == a || RANKING[start + i] == b) { continue; } if (RANKING[start + i] < a) { smaller++; } else if (RANKING[start + i] > b) { larger++; } else { if (start + i > a_pos && start + i < b_pos) { return 0; } middle++; } // DEBUG // fprintf(stderr, // "a: %d, b: %d, a_pos: %d, b_pos: %d, i: %d, larger: %d, // smaller: " // "%d, middle: %d\n", // a, b, a_pos, b_pos, i, larger, smaller, middle); } if (larger == smaller && middle == 0) { result++; } for (int i = start + 1; i < std::min(a_pos + 1, (int)RANKING.size() - n + 1); ++i) { if (RANKING[i - 1] < a) { smaller--; } else if (RANKING[i - 1] > b) { larger--; } else { middle--; } if (RANKING[i + n - 1] < a) { smaller++; } else if (RANKING[i + n - 1] > b) { larger++; } else { middle++; } if (larger == smaller && middle == 0) { result++; } } // DEBUG // fprintf(stderr, "result: %d\n", result); return result; } int process(const int N, const int MAX, const std::vector<int> &POSITIONS, const std::vector<int> &RANKING) { int result = 1; for (int n = N - 1; n > 0; --n) { if ((MAX - n) % 2) { for (int b = N; b > (MAX - n) / 2; --b) { result += check_two(MAX - n - b, b, n, POSITIONS, RANKING); } } else { result += check_one((MAX - n) / 2, n, POSITIONS, RANKING); } } return result; } int main() { int N = 0; std::cin >> N; std::vector<int> v(N); for (int i = 0; i < N; ++i) { std::cin >> v[i]; } /////////////////// const int MAX = 2 * N + 1; const std::vector<int> POSITIONS = get_number_postions(v); // DEBUG // std::cerr << "max: " << MAX << std::endl; std::cout << MAX << " " << process(N, MAX, POSITIONS, v) << std::endl; return 0; } |