제출 #959843

#제출 시각아이디문제언어결과실행 시간메모리
959843kilkuwu서열 (APIO23_sequence)C++17
50 / 100
2059 ms55232 KiB
#include "sequence.h" #include <bits/stdc++.h> #ifdef LOCAL #include "template/dbgnc.hpp" #else #define dbg(...) ; #define timer(...) ; #endif struct Tag { int add = 0; void apply(const Tag& t) { add += t.add; } }; struct Info { int mn = 0, mx = 0; void apply(const Tag& t) { mn += t.add, mx += t.add; } }; Info operator+(const Info& a, const Info& b) { return {std::min(a.mn, b.mn), std::max(a.mx, b.mx)}; } struct SegmentTree { int n; std::vector<Info> info; std::vector<Tag> tag; SegmentTree(int _n) : n(_n), info(4 * n), tag(4 * n) {} void apply(int k, const Tag& t) { info[k].apply(t); tag[k].apply(t); } void push(int k) { apply(k * 2, tag[k]); apply(k * 2 + 1, tag[k]); tag[k] = Tag(); } void update(int k, int l, int r, int ql, int qr, const Tag& t) { if (l > qr || r < ql) return; if (ql <= l && r <= qr) { info[k].apply(t); tag[k].apply(t); return; } push(k); int mid = (l + r) / 2; update(k * 2, l, mid, ql, qr, t); update(k * 2 + 1, mid + 1, r, ql, qr, t); info[k] = info[k * 2] + info[k * 2 + 1]; } Info query(int k, int l, int r, int ql, int qr) { if (l > qr || r < ql) return {(int) 1e9, (int) -1e9}; if (ql <= l && r <= qr) { return info[k]; } push(k); int mid = (l + r) / 2; return query(k * 2, l, mid, ql, qr) + query(k * 2 + 1, mid + 1, r, ql, qr); } void update(int l, int r, const Tag& t) { update(1, 0, n - 1, l, r, t); } Info query(int l, int r) { return query(1, 0, n - 1, l, r); } }; int sequence(int N, std::vector<int> A) { std::vector<std::vector<int>> pos(N); for (int i = 0; i < N; i++) { pos[--A[i]].push_back(i); } auto check = [&](int mid) { SegmentTree tree(N + 1); for (int i = 0; i < N; i++) { tree.update(i + 1, N, {1}); } for (int v = 0; v < N; v++) { for (int i : pos[v]) { tree.update(i + 1, N, {-2}); } int sz = pos[v].size(); for (int i = sz - 1; i >= 0; i--) { int l = pos[v][i]; tree.update(l + 1, N, {2}); if (i + mid <= sz) { int r = pos[v][i + mid - 1]; auto rl = tree.query(0, l); int l1 = rl.mn, r1 = rl.mx; auto rr = tree.query(r + 1, N); int l2 = rr.mn - 2 * mid, r2 = rr.mx; if (std::max(l1, l2) <= std::min(r1, r2)) return true; } } for (int i : pos[v]) { tree.update(i + 1, N, {-2}); // becomes -1 } } return false; }; int lb = 1, rb = N; while (lb < rb) { int mid = (lb + rb + 1) / 2; if (check(mid)) { lb = mid; } else { rb = mid - 1; } } return lb; } #ifdef LOCAL int main() { int N; assert(1 == scanf("%d", &N)); std::vector<int> A(N); for (int i = 0; i < N; ++i) { assert(1 == scanf("%d", &A[i])); } int result = sequence(N, A); printf("%d\n", result); return 0; } #endif
#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...