#include <sstream> #include <vector> #include <iostream> #include <list> #include <algorithm> #include <queue> #include <fstream> #include <chrono> using namespace std; /* 5 1 4 3 5 2 */ class MedianHolder { priority_queue<int, vector<int>, greater<int> > minHeap; priority_queue<int, vector<int>> maxHeap; int median; bool isEven = true; public: int size() { if (isEven) { return minHeap.size() + maxHeap.size(); } return minHeap.size() + maxHeap.size() + 1; } int getDoubleMedian() { if (isEven) { return minHeap.top() + maxHeap.top(); } else { return median * 2; } } void addElem(int elem) { if (isEven && maxHeap.empty()) { median = elem; isEven = false; } else if (isEven) { int smallerPart = maxHeap.top(); int biggerPart = minHeap.top(); if (elem < smallerPart) { maxHeap.pop(); maxHeap.push(elem); median = smallerPart; } else if (elem > biggerPart) { minHeap.pop(); minHeap.push(elem); median = biggerPart; } else { median = elem; } isEven = false; } else { if (elem > median) { maxHeap.push(median); minHeap.push(elem); } else { maxHeap.push(elem); minHeap.push(median); } median = -1; isEven = true; } } }; struct Result { int best; int ways; }; Result ranking(int arr[], int n) { Result result; result.best = -1; result.ways = -1; int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } for (int l = 0; l < n; l++) { MedianHolder medianHolder; for (int r = l; r < n; r++) { medianHolder.addElem(arr[r]); int doubleMedian = medianHolder.getDoubleMedian(); int rankingValue = medianHolder.size() + doubleMedian; if (rankingValue > result.best) { result.best = rankingValue; result.ways = 1; } else if (rankingValue == result.best) { result.ways++; } } } return result; } int main() { // auto start = chrono::high_resolution_clock::now(); // // std::ifstream in_file("/Users/pro15/Documents/sklep_test_10000.in"); // std::cin.rdbuf(in_file.rdbuf()); //redirect std::cin to in_file! int n, i; cin >> n; int array[n]; for (i = 0; i < n; i++) { cin >> array[i]; } Result result = ranking(array, n); cout << result.best << " " << result.ways << endl; // auto stop = chrono::high_resolution_clock::now(); // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start); // cout << "time micro seconds: " << duration.count() << 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 | #include <sstream> #include <vector> #include <iostream> #include <list> #include <algorithm> #include <queue> #include <fstream> #include <chrono> using namespace std; /* 5 1 4 3 5 2 */ class MedianHolder { priority_queue<int, vector<int>, greater<int> > minHeap; priority_queue<int, vector<int>> maxHeap; int median; bool isEven = true; public: int size() { if (isEven) { return minHeap.size() + maxHeap.size(); } return minHeap.size() + maxHeap.size() + 1; } int getDoubleMedian() { if (isEven) { return minHeap.top() + maxHeap.top(); } else { return median * 2; } } void addElem(int elem) { if (isEven && maxHeap.empty()) { median = elem; isEven = false; } else if (isEven) { int smallerPart = maxHeap.top(); int biggerPart = minHeap.top(); if (elem < smallerPart) { maxHeap.pop(); maxHeap.push(elem); median = smallerPart; } else if (elem > biggerPart) { minHeap.pop(); minHeap.push(elem); median = biggerPart; } else { median = elem; } isEven = false; } else { if (elem > median) { maxHeap.push(median); minHeap.push(elem); } else { maxHeap.push(elem); minHeap.push(median); } median = -1; isEven = true; } } }; struct Result { int best; int ways; }; Result ranking(int arr[], int n) { Result result; result.best = -1; result.ways = -1; int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } for (int l = 0; l < n; l++) { MedianHolder medianHolder; for (int r = l; r < n; r++) { medianHolder.addElem(arr[r]); int doubleMedian = medianHolder.getDoubleMedian(); int rankingValue = medianHolder.size() + doubleMedian; if (rankingValue > result.best) { result.best = rankingValue; result.ways = 1; } else if (rankingValue == result.best) { result.ways++; } } } return result; } int main() { // auto start = chrono::high_resolution_clock::now(); // // std::ifstream in_file("/Users/pro15/Documents/sklep_test_10000.in"); // std::cin.rdbuf(in_file.rdbuf()); //redirect std::cin to in_file! int n, i; cin >> n; int array[n]; for (i = 0; i < n; i++) { cin >> array[i]; } Result result = ranking(array, n); cout << result.best << " " << result.ways << endl; // auto stop = chrono::high_resolution_clock::now(); // auto duration = chrono::duration_cast<chrono::microseconds>(stop - start); // cout << "time micro seconds: " << duration.count() << endl; return 0; } |