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>
#include <algorithm>
#include <map>
using namespace std;
// Helper function to add element to the multiset and update the frequency map
void add_element(multiset<int>& lower, multiset<int>& upper, map<int, int>& freq, int element) {
if (lower.empty() || element <= *lower.rbegin()) {
lower.insert(element);
} else {
upper.insert(element);
}
// Balance the sets
if (lower.size() > upper.size() + 1) {
upper.insert(*lower.rbegin());
lower.erase(prev(lower.end()));
} else if (upper.size() > lower.size()) {
lower.insert(*upper.begin());
upper.erase(upper.begin());
}
// Update frequency
freq[element]++;
}
// Helper function to remove element from the multiset and update the frequency map
void remove_element(multiset<int>& lower, multiset<int>& upper, map<int, int>& freq, int element) {
if (lower.find(element) != lower.end()) {
lower.erase(lower.find(element));
} else {
upper.erase(upper.find(element));
}
// Balance the sets
if (lower.size() > upper.size() + 1) {
upper.insert(*lower.rbegin());
lower.erase(prev(lower.end()));
} else if (upper.size() > lower.size()) {
lower.insert(*upper.begin());
upper.erase(upper.begin());
}
// Update frequency
freq[element]--;
if (freq[element] == 0) {
freq.erase(element);
}
}
// Function to get the median from the two balanced sets
int get_median(const multiset<int>& lower, const multiset<int>& upper) {
return *lower.rbegin();
}
int sequence(int N, vector<int> A) {
int maxOccurrences = 0;
for (int l = 0; l < N; ++l) {
multiset<int> lower, upper;
map<int, int> freq;
for (int r = l; r < N; ++r) {
add_element(lower, upper, freq, A[r]);
int median = get_median(lower, upper);
maxOccurrences = max(maxOccurrences, freq[median]);
}
}
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... |