#include <iostream> #include <algorithm> #include <vector> #include <utility> #include <algorithm> #include <time.h> int median(const std::vector<int> &numbers) { if (numbers.size() % 2 == 1) { return 2 * numbers[numbers.size() / 2]; } else { return numbers[numbers.size() / 2 - 1] + numbers[numbers.size() / 2]; } } int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> numbers(n + 1), index(n + 1); for (int i = 1; i <= n; i++) { int v; std::cin >> v; numbers[i - 1] = v; index[v] = i; } if (n == 1) { puts("3 1"); return 0; } int next = n - 1; long long result = 2; std::pair<int,int> range = {index[n], index[n]}; for (int i = n - 1; i > 1; --i) { if ((n - i) % 2 == 1) { range = {std::min(range.first, index[next]), std::max(range.second, index[next])}; --next; } int length = range.second - range.first + 1; if (length > n - i + 1) { continue; } int remaining = n - i - length + 1; int left = std::max(0, remaining + range.second - n); int right = std::min(remaining, range.first - 1); result += (right - left + 1); } std::cout << 2 * n + 1 << " " << result << '\n'; 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 | #include <iostream> #include <algorithm> #include <vector> #include <utility> #include <algorithm> #include <time.h> int median(const std::vector<int> &numbers) { if (numbers.size() % 2 == 1) { return 2 * numbers[numbers.size() / 2]; } else { return numbers[numbers.size() / 2 - 1] + numbers[numbers.size() / 2]; } } int main() { std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> numbers(n + 1), index(n + 1); for (int i = 1; i <= n; i++) { int v; std::cin >> v; numbers[i - 1] = v; index[v] = i; } if (n == 1) { puts("3 1"); return 0; } int next = n - 1; long long result = 2; std::pair<int,int> range = {index[n], index[n]}; for (int i = n - 1; i > 1; --i) { if ((n - i) % 2 == 1) { range = {std::min(range.first, index[next]), std::max(range.second, index[next])}; --next; } int length = range.second - range.first + 1; if (length > n - i + 1) { continue; } int remaining = n - i - length + 1; int left = std::max(0, remaining + range.second - n); int right = std::min(remaining, range.first - 1); result += (right - left + 1); } std::cout << 2 * n + 1 << " " << result << '\n'; return 0; } |