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"
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ...b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 500005
#define ff first
#define ss second
#define pii pair<int, int>
const int inf = 1<<30;
struct segtree{
int ma[4*maxn], mi[4*maxn], tag[4*maxn], tag2[4*maxn];
void init(int cur, int l, int r) {
if (r <= l) return;
tag[cur] = 0;
if (r - l == 1) {
ma[cur] = mi[cur] = l+1;
return;
}
int m = (l + r) / 2;
init(cur*2, l, m), init(cur*2+1, m, r);
ma[cur] = max(ma[cur*2], ma[cur*2+1]);
mi[cur] = min(mi[cur*2], mi[cur*2+1]);
}
void push(int cur, int l, int r) {
ma[cur] += tag[cur];
mi[cur] += tag[cur];
if (r - l > 1) {
tag[cur*2] += tag[cur];
tag[cur*2+1] += tag[cur];
}
tag[cur] = 0;
}
void modify(int cur, int l, int r, int ql, int qr, int v) {
if (r <= l || ql >= r || qr <= l) return;
push(cur, l, r);
if (ql <= l && qr >= r) {
tag[cur] += v;
return;
}
int m = (l + r) / 2;
modify(cur*2, l, m, ql, qr, v);
modify(cur*2+1, m, r, ql, qr, v);
push(cur*2, l, m), push(cur*2+1, m, r);
ma[cur] = max(ma[cur*2], ma[cur*2+1]);
mi[cur] = min(mi[cur*2], mi[cur*2+1]);
}
pii query(int cur, int l, int r, int ql, int qr) { //(ma, mi)
if (r <= l || ql >= r || qr <= l) return {-inf, inf};
push(cur, l, r);
if (ql <= l && qr >= r) return make_pair(ma[cur], mi[cur]);
int m = (l + r) / 2;
pii pl = query(cur*2, l, m, ql, qr), pr = query(cur*2+1, m, r, ql, qr);
pl.ff = max(pl.ff, pr.ff);
pl.ss = min(pl.ss, pr.ss);
return pl;
}
int get(int cur, int l, int r, int x) {
if (r <= l) return 0;
push(cur, l, r);
if (r - l == 1) return ma[cur];
int m = (l + r) / 2;
if (x < m) return get(cur*2, l, m, x);
else return get(cur*2+1, m, r, x);
}
} pref, pref2;
vector<int> pos[maxn];
int sequence(int N, std::vector<int> A) {
pref.init(1, 0, N);
pref2.init(1, 0, N);
for (int i = 0;i < N;i++) {
pos[A[i]].push_back(i);
}
//for (int j = 0;j < N;j++) cout << pref.get(1,0, N, j) << " ";
//cout << endl;
int ans = 1;
for (int i = 1;i <= N;i++) {
int siz = pos[i].size();
if (siz == 0) continue;
for (int j = 0;j < siz;j++) {
pref.modify(1, 0, N, pos[i][j], N, -2);
}
//for (int j = 0;j < N;j++) cout << pref.get(1,0, N, j) << " ";
//cout << endl;
auto check = [&] (int ind, int j) {
int l = pos[i][ind], r = pos[i][j], cnt = j - ind + 1; //[l, r]
pii pl = pref.query(1, 0, N, 0, l), pr = pref.query(1, 0, N, r, N);
pl.ff = max(pl.ff, 0), pl.ss = min(pl.ss, 0);
pii p = {pr.ff - pl.ss, pr.ss - pl.ff};
pl = pref2.query(1, 0, N, 0, l), pr = pref2.query(1, 0, N, r, N);
pl.ff = max(pl.ff, 0), pl.ss = min(pl.ss, 0);
pii p2 = {pr.ff - pl.ss, pr.ss - pl.ff};
//debug(l, r, p.ss, p2.ff);
//debug(ind, j, p.ss, p.ff);
if ((p.ss <= 0 && 0 <= p2.ff)) {
ans = max(ans, j - ind + 1);
return true;
} else {
return false;
}
};
int low = 1, up = siz+1;
while (low < up - 1) {
int mid = (low + up) / 2;
int poss = 0;
for (int x = 0;x <= siz - mid;x++) {
if (check(x, x + mid - 1)) {
poss = 1;
break;
}
}
if (poss) low = mid;
else up = mid;
}
for (int j = 0;j < siz;j++) {
pref2.modify(1, 0, N, pos[i][j], N, -2);
}
}
return ans;
}
Compilation message (stderr)
sequence.cpp: In lambda function:
sequence.cpp:101:40: warning: unused variable 'cnt' [-Wunused-variable]
101 | int l = pos[i][ind], r = pos[i][j], cnt = j - ind + 1; //[l, r]
| ^~~
# | 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... |