이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
int type, N;
void init(int cur, int l, int r) {
if (r <= l) return;
tag[cur] = 0;
if (r - l == 1) {
ma[cur] = mi[cur] = type ? N - l : 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) {
if (!tag[cur]) return;
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, suf;
vector<int> pos[maxn];
int sequence(int N, std::vector<int> A) {
pref.type = 0, suf.type = 1;
pref.N = suf.N = N;
pref.init(1, 0, N), suf.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;
vector<int> pv(N), sv(N);
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, -1);
suf.modify(1, 0, N, 0, pos[i][j]+1, -1);
}
for (int j = 0;j < siz;j++) {
pv[pos[i][j]] = pref.get(1, 0, N, pos[i][j]);
sv[pos[i][j]] = suf.get(1, 0, N, pos[i][j]);
}
//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]
int sum = pv[r] - pref.get(1, 0, N, l);
pii pl = pref.query(1, 0, N, 0, l), pr = pref.query(1, 0, N, r+1, N);
pii p = {sum - min(0, pl.ss) + max(0, pr.ff-pv[r]), sum - max(0, pl.ff) + min(0, pr.ss-pv[r])};
//debug(ind, j, p.ss, p.ff);
if (max(p.ss, -cnt) <= min(p.ff, cnt)) {
ans = max(ans, j - ind + 1);
return true;
} else {
return false;
}
};
for (int x = 0;x < siz;x++) {
for (int y = x+ans;y < siz;y++) check(x, y);
}
/*
int low = 1, up = siz+1;
while (low < up - 1) {
//int mid = (low + up) / 2;
int mid = low+1;
bool 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++) {
pref.modify(1, 0, N, pos[i][j], N, -1);
suf.modify(1, 0, N, 0, pos[i][j]+1, -1);
}
}
return 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... |