이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 5e5 + 100;
const int M = 2e6;
struct FT{
vector <int> f;
int N;
FT(int n) {
N = n;
f.assign(n + 1, 0);
}
void upd(int i, int x) {
for (; i <= N; i += i & (-i))
f[i] += x;
}
int get(int i) {
int s = 0;
for (; i; i -= i & (-i))
s += f[i];
return s;
}
};
struct seg{
int N;
vector <int> st;
seg(int n) {
N = n;
st.assign(n * 4 + 1000, 1e9);
}
void upd(int id, int l, int r, int p, int v, int is) {
if (l > p || r < p)
return;
if (l == r) {
if (is) {
st[id] = v;
} else {
st[id] = min(st[id], v);
}
return;
}
int m = (l + r) / 2;
upd(id * 2, l, m, p, v, is);
upd(id * 2 + 1, m + 1, r, p, v, is);
st[id] = min(st[id * 2], st[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u)
return 1e9;
if (l >= u && r <= v)
return st[id];
int m = (l + r) / 2;
return min(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
};
vector <int> pos[N];
int a[N], n;
void maximize(int &a, int b) {
if (a < b) {
a = b;
}
}
int get(int add) {
seg seg(n * 2);
FT P(n + 100);
for (int i = 1; i <= n; i++) {
P.upd(i, add);
}
int ans = 0, t = 0;
for (int i = 1; i <= n; i++) {
for (auto x : pos[i]) {
P.upd(x, -add);
}
vector <pair <int, int>> st;
for (int j = 0; j < sz(pos[i]); j++) {
if ((j && pos[i][j] - 1 != pos[i][j - 1]) || (j == 0))
st.pb({j, P.get(pos[i][j] - 1)});
st.pb({j + 1, P.get(pos[i][j])});
if ((j < sz(pos[i]) - 1 && pos[i][j] + 1 != pos[i][j + 1]) || (j == sz(pos[i]) - 1))
st.pb({j + 1, P.get(pos[i][j] + 1)});
}
st.pb({0, 0});
st.pb({sz(pos[i]), P.get(n)});
uniquev(st);
sort(all(st), [&] (pair <int, int> &A, pair <int, int> &B) {return A.s < B.s;} );
for (auto x : st) {
maximize(ans, x.f - seg.get(1, 0, n * 2, x.s - x.f + n, n * 2));
seg.upd(1, 0, n * 2, x.s - x.f + n, x.f, 0);
}
for (auto x : st) {
seg.upd(1, 0, n * 2, x.s - x.f + n, 1e9, 1);
}
for (auto x : pos[i]) {
P.upd(x, -add);
}
}
return ans;
}
int sequence(int N, vector <int> A) {
n = N;
for (int i = 1; i <= n; i++)
pos[i].clear();
for (int i = 1; i <= n; i++) {
a[i] = A[i - 1];
pos[a[i]].pb(i);
}
int ans = max(get(-1), get(1));
return ans;
}
/*int main() {
cout << sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2}) << '\n';
cout << sequence(9, {1, 1, 2, 3, 4, 3, 2, 1, 1}) << '\n';
cout << sequence(14, {2, 6, 2, 5, 3, 4, 2, 1, 4, 3, 5, 6, 3, 2}) << '\n';
cout << sequence(3, {3, 3, 3}) << '\n';
return 0;
}*/
// s[i] - s[j] <= p[i] - p[j]
// s[i] - p[i] <= s[j] - p[j]
// s[i] - i >= s[j] - j
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int get(int)':
sequence.cpp:103:18: warning: unused variable 't' [-Wunused-variable]
103 | int ans = 0, t = 0;
| ^
# | 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... |