#include <iostream> constexpr int MAX_N = 1000000; int positionLUT[MAX_N + 1]; int positionOfMax = 0; int n; long long waysNumber = 1; inline void readInput() { std::cin >> n; for (int i = 0; i < n; i++) { int input; std::cin >> input; positionLUT[input] = i; if (input == n) positionOfMax = i; } } inline void calculateDistances() { for (int i = 1; i <= n; i++) { positionLUT[i] = positionLUT[i] - positionOfMax; } } inline void calculatedWaysNumber() { int positionRight = 0; int positionLeft = 0; int range = positionRight - positionLeft + 1; int lastChecked = n; for (int i = 2; i <= n; i++) { int rangeLength = i; if (rangeLength % 2 == 0) { lastChecked--; if (positionLUT[lastChecked] < 0) { positionLeft = std::min(positionLUT[lastChecked], positionLeft); } if (positionLUT[lastChecked] > 0) { positionRight = std::max(positionLUT[lastChecked], positionRight); } range = positionRight - positionLeft + 1; } if (i >= range) { int neededSpace = i - range; int availableLeft = positionOfMax + positionLeft; // TODO int availableRight = n - (positionOfMax + positionRight) - 1; // TODO int lowerAvailable = std::min(availableLeft, availableRight); int higherAvailable = std::max(availableLeft, availableRight); int limitedWaysMax = std::min((lowerAvailable + 1), neededSpace + 1) + std::min((higherAvailable + 1), neededSpace + 1) - (neededSpace + 1); waysNumber += std::min(neededSpace + 1, limitedWaysMax); } } } void solve() { readInput(); calculateDistances(); calculatedWaysNumber(); std::cout << n * 2 + 1 << " " << waysNumber; } int main() { 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 72 73 | #include <iostream> constexpr int MAX_N = 1000000; int positionLUT[MAX_N + 1]; int positionOfMax = 0; int n; long long waysNumber = 1; inline void readInput() { std::cin >> n; for (int i = 0; i < n; i++) { int input; std::cin >> input; positionLUT[input] = i; if (input == n) positionOfMax = i; } } inline void calculateDistances() { for (int i = 1; i <= n; i++) { positionLUT[i] = positionLUT[i] - positionOfMax; } } inline void calculatedWaysNumber() { int positionRight = 0; int positionLeft = 0; int range = positionRight - positionLeft + 1; int lastChecked = n; for (int i = 2; i <= n; i++) { int rangeLength = i; if (rangeLength % 2 == 0) { lastChecked--; if (positionLUT[lastChecked] < 0) { positionLeft = std::min(positionLUT[lastChecked], positionLeft); } if (positionLUT[lastChecked] > 0) { positionRight = std::max(positionLUT[lastChecked], positionRight); } range = positionRight - positionLeft + 1; } if (i >= range) { int neededSpace = i - range; int availableLeft = positionOfMax + positionLeft; // TODO int availableRight = n - (positionOfMax + positionRight) - 1; // TODO int lowerAvailable = std::min(availableLeft, availableRight); int higherAvailable = std::max(availableLeft, availableRight); int limitedWaysMax = std::min((lowerAvailable + 1), neededSpace + 1) + std::min((higherAvailable + 1), neededSpace + 1) - (neededSpace + 1); waysNumber += std::min(neededSpace + 1, limitedWaysMax); } } } void solve() { readInput(); calculateDistances(); calculatedWaysNumber(); std::cout << n * 2 + 1 << " " << waysNumber; } int main() { solve(); return 0; } |