This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sequence.h"
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
void balanceHeaps(multiset<int> &maxHeap, multiset<int> &minHeap) {
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.insert(*maxHeap.rbegin());
maxHeap.erase(--maxHeap.end());
} else if (minHeap.size() > maxHeap.size()) {
maxHeap.insert(*minHeap.begin());
minHeap.erase(minHeap.begin());
}
}
int sequence(int N, vector<int> A) {
int maxOccurrences = 0;
for (int l = 0; l < N; ++l) {
unordered_map<int, int> freq;
multiset<int> minHeap, maxHeap;
for (int r = l; r < N; ++r) {
int num = A[r];
if (maxHeap.empty() || num <= *maxHeap.rbegin()) {
maxHeap.insert(num);
} else {
minHeap.insert(num);
}
balanceHeaps(maxHeap, minHeap);
freq[num]++;
int median1 = *maxHeap.rbegin();
int median2 = (maxHeap.size() + minHeap.size()) % 2 == 0 ? *minHeap.begin() : median1;
maxOccurrences = max(maxOccurrences, freq[median1]);
if (median1 != median2) {
maxOccurrences = max(maxOccurrences, freq[median2]);
}
}
}
return maxOccurrences;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |