#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { std::ios::sync_with_stdio(false); int numberOfRatings; cin >> numberOfRatings; vector<int> rating(numberOfRatings); vector<int> indexOfRating(numberOfRatings + 1); for (int i = 0; i < numberOfRatings; i++) { cin >> rating[i]; indexOfRating[rating[i]] = i; } if (numberOfRatings == 1) { cout << 3 << " " << 1; return 0; } long long int possibleSolutions = 1; int left = indexOfRating[numberOfRatings]; int right = left; for (int length = 2; length <= numberOfRatings; length++) { int indexOfNewTopKRating = indexOfRating[numberOfRatings - length / 2]; left = min(left, indexOfNewTopKRating); right = max(right, indexOfNewTopKRating); int numberOfElements = right - left + 1; int numberOfFreeSlots = length - numberOfElements; int numberOfElementsOnLeft = left; int numberOfElementsOnRight = numberOfRatings - 1 - right; if (numberOfFreeSlots == 0) { possibleSolutions++; } else if (numberOfFreeSlots > 0) { possibleSolutions += min(numberOfElementsOnLeft, numberOfFreeSlots) + min(numberOfElementsOnRight, numberOfFreeSlots) - numberOfFreeSlots + 1; } } cout << numberOfRatings * 2 + 1 << " " << possibleSolutions; }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { std::ios::sync_with_stdio(false); int numberOfRatings; cin >> numberOfRatings; vector<int> rating(numberOfRatings); vector<int> indexOfRating(numberOfRatings + 1); for (int i = 0; i < numberOfRatings; i++) { cin >> rating[i]; indexOfRating[rating[i]] = i; } if (numberOfRatings == 1) { cout << 3 << " " << 1; return 0; } long long int possibleSolutions = 1; int left = indexOfRating[numberOfRatings]; int right = left; for (int length = 2; length <= numberOfRatings; length++) { int indexOfNewTopKRating = indexOfRating[numberOfRatings - length / 2]; left = min(left, indexOfNewTopKRating); right = max(right, indexOfNewTopKRating); int numberOfElements = right - left + 1; int numberOfFreeSlots = length - numberOfElements; int numberOfElementsOnLeft = left; int numberOfElementsOnRight = numberOfRatings - 1 - right; if (numberOfFreeSlots == 0) { possibleSolutions++; } else if (numberOfFreeSlots > 0) { possibleSolutions += min(numberOfElementsOnLeft, numberOfFreeSlots) + min(numberOfElementsOnRight, numberOfFreeSlots) - numberOfFreeSlots + 1; } } cout << numberOfRatings * 2 + 1 << " " << possibleSolutions; } |