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 <set>
#include <iostream>
using namespace std;
int sequence(int N, vector<int> A) {
int maxOccurrences = 0;
// Iterate over each possible starting point of subarray
for (int l = 0; l < N; ++l) {
unordered_map<int, int> freq; // Frequency map for current subarray
multiset<int> lower, upper; // Two multisets to manage medians
// Iterate over each possible ending point of subarray starting from 'l'
for (int r = l; r < N; ++r) {
int num = A[r];
// Insert the number into one of the heaps
if (lower.empty() || num <= *lower.rbegin()) {
lower.insert(num);
} else {
upper.insert(num);
}
// Balance the heaps
while (lower.size() > upper.size() + 1) {
upper.insert(*lower.rbegin());
lower.erase(prev(lower.end()));
}
while (upper.size() > lower.size()) {
lower.insert(*upper.begin());
upper.erase(upper.begin());
}
// Update frequency map
freq[num]++;
// Find the current medians
int median1 = *lower.rbegin();
int median2 = (lower.size() + upper.size()) % 2 == 0 ? *upper.begin() : median1;
// Update maxOccurrences with frequency of the current medians
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... |