이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using vi = vector <int>;
using ii = pair <ll, ll>;
using vii = vector <ii>;
struct MedianDS {
multiset <ll, greater <ll> > st1; // big to small (mid...0)
multiset <ll> st2; // small to big (mid...n)
vll freq;
MedianDS (ll n): freq(n, 0) {}
void stabilize () {
while (st2.size() > st1.size()) {
st1.insert(*st2.begin());
st2.erase(st2.begin());
}
while (st1.size() > st2.size()) {
st2.insert(*st1.begin());
st1.erase(st1.begin());
}
}
void insert (ll x) {
ll mid = (st2.size() ? *st2.begin() : 0);
if (x >= mid) st2.insert(x);
else st1.insert(x);
freq[x]++;
stabilize();
}
void erase (ll x) {
ll mid = (st2.size() ? *st2.begin() : 0);
if (x >= mid) st2.erase(st2.find(x));
else st1.erase(st1.find(x));
freq[x]--;
stabilize();
}
ll query () {
if (st1.size() == st2.size()) { // two medians
ll med1 = *st1.begin();
ll med2 = *st2.begin();
return max(freq[med1], freq[med2]);
} else { // one median
assert(st2.size() > st1.size());
ll med = *st2.begin();
return freq[med];
}
}
};
int sequence (int n, vi ve) {
for (int &i : ve) i--;
ll ans = 1;
deque <ll> dqs[n];
for (ll i = 0; i < n; i++) {
dqs[ve[i]].push_back(i);
}
for (ll i = 0; i < n; i++) {
deque <ll> &dq = dqs[i];
if (dq.size() < 2) continue;
MedianDS ds(n);
ll l = dq.front(), r = dq.back();
for (ll j = l; j <= r; j++) {
ds.insert(ve[j]);
}
ans = max(ans, ds.query());
while (dq.size() >= 2) {
ll disF = dq[1]-dq[0];
ll disB = dq[dq.size()-1]-dq[dq.size()-2];
if (disF > disB) {
dq.pop_front();
while (l++ < dq.front()) ds.erase(ve[l-1]);
} else {
dq.pop_back();
while (r-- > dq.back()) ds.erase(ve[r+1]);
}
ans = max(ans, ds.query());
}
}
return int(ans);
}
# | 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... |