제출 #982051

#제출 시각아이디문제언어결과실행 시간메모리
982051vjudge1서열 (APIO23_sequence)C++17
0 / 100
2051 ms373408 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...