이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 5e5 + 5;
int n, ret = 1;
int a[maxn];
int check(int e, int x) {
multiset<int> s;
vector<int> pref(n, 0);
int flag = 0;
int hi = 0, cnt = 0, val = 0;
s.insert(0);
for(int i = 0;i < n;i++) {
while(1) {
if(hi == n) break;
if(cnt > e) break;
if(a[hi] == x) cnt++;
if(a[hi] > x) val++;
if(a[hi] < x) val--;
pref[hi] = val;
if(cnt == e) {
auto it1 = s.lower_bound(val);
auto it2 = it1;
if(it2 != s.begin()) it2--;
if(abs(*it1 - val) <= e) flag = 1;
if(abs(*it2 - val) <= e) flag = 1;
}
hi++;
}
s.insert(pref[i]);
if(a[i] == x) cnt--;
}
return flag;
}
void solve(int x) {
int lo = 0, hi = n, ans = -1;
while(lo <= hi) {
int mid = (lo + hi) / 2;
if(check(mid, x)) ans = mid, lo = mid + 1;
else hi = mid - 1;
}
ret = max(ret, ans);
}
int sequence(int N, vector<int> A) {
n = N;
for(int i = 0;i < n;i++) a[i] = A[i];
int cnt = 1;
for(int i = 1;i < n;i++) {
if(a[i] == a[i - 1]) cnt++;
else cnt = 1;
ret = max(ret, cnt);
}
for(int i = 1;i <= 3;i++) solve(i);
return ret;
}
# | 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... |