#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
template <class InIter, class OutIter> void compress(InIter first, InIter last, OutIter result) { vector <__typeof(*first)> v(first, last); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); while (first != last) { *result = lower_bound(v.begin(), v.end(), *first) - v.begin(); ++first; ++result; } }
// end of template
const int MAX = 5e5 + 5;
int N;
int ma[MAX << 2], lazy[MAX << 2], mi[MAX << 2];
void pushDown(int i) {
if (!lazy[i]) return;
ma[i << 1] += lazy[i];
ma[i << 1 | 1] += lazy[i];
mi[i << 1] += lazy[i];
mi[i << 1 | 1] += lazy[i];
lazy[i << 1] += lazy[i];
lazy[i << 1 | 1] += lazy[i];
lazy[i] = 0;
}
void update(int i, int l, int r, int u, int v, int add) {
if (l >= v || r <= u) return;
if (u <= l && r <= v) {
ma[i] += add;
mi[i] += add;
lazy[i] += add;
return;
}
pushDown(i);
int m = l + r >> 1;
update(i << 1, l, m, u, v, add);
update(i << 1 | 1, m, r, u, v, add);
ma[i] = max(ma[i << 1], ma[i << 1 | 1]);
mi[i] = min(mi[i << 1], mi[i << 1 | 1]);
}
pair <int, int> getRange(int i, int l, int r, int u, int v) {
if (l >= v || r <= u) return make_pair(N + N + N, -N - N - N);
if (u <= l && r <= v) return make_pair(mi[i], ma[i]);
pushDown(i);
int m = l + r >> 1;
pair <int, int> x = getRange(i << 1, l, m, u, v);
pair <int, int> y = getRange(i << 1 | 1, m, r, u, v);
return make_pair(min(x.fi, y.fi), max(x.se, y.se));
}
int sequence(int n, vector <int> A) {
N = n;
vector <int> cnt(N);
REP(i, N) ++cnt[--A[i]];
vector <vector <int>> pos(N);
REP(i, N) pos[A[i]].push_back(i);
REP(i, N) update(1, 0, N, i, N, 1);
int res = 1;
REP(i, N) {
for (int x: pos[i]) update(1, 0, N, x, N, -1);
for (int j = 0; j + res < (int) pos[i].size(); ++j) {
while (j + res < (int) pos.size()) {
int l = pos[i][j], r = pos[i][j + res];
pair <int, int> fl = getRange(1, 0, N, 0, l);
minimize(fl.fi, 0);
maximize(fl.se, 0);
pair <int, int> fr = getRange(1, 0, N, r, N);
if (min(fl.se, fr.se + res + 1) >= max(fl.fi, fr.fi - res - 1)) ++res;
else break;
}
}
for (int x: pos[i]) update(1, 0, N, x, N, -1);
}
return res;
}
#ifdef LOCAL
#include "sequence.h"
#include <cassert>
#include <cstdio>
#include <vector>
int main() {
file("sequence");
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
Compilation message
sequence.cpp: In function 'void update(int, int, int, int, int, int)':
sequence.cpp:70:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
70 | int m = l + r >> 1;
| ~~^~~
sequence.cpp: In function 'std::pair<int, int> getRange(int, int, int, int, int)':
sequence.cpp:81:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
218 ms |
81232 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
536 ms |
45908 KB |
Output is correct |
2 |
Correct |
528 ms |
45892 KB |
Output is correct |
3 |
Incorrect |
556 ms |
45328 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |