Solution S o l u t i o n
考虑
j≤i
j
≤
i
和
j>i
j
>
i
。
ansi=max{aj+|i−j|−−−−−√−ai}
a
n
s
i
=
max
{
a
j
+
|
i
−
j
|
−
a
i
}
有决策单调性,单调队列。
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pairs;
const int N = 505050;
inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
int n;
int a[N], ans[N];
double f[N];
struct tri {
int pos, l, r;
tri(int p = 0, int _l = 0, int _r = 0): pos(p), l(_l), r(_r) {}
} q[N];
int h, t;
inline int st(int x) {
return ceil(sqrt(x));
}
inline double value(int i, int j) {
return a[j] + sqrt(fabs(i - j)) - a[i];
}
inline int find(int i, int pos, int L, int R) {
int Mid, t;
while (L <= R) {
Mid = (L + R) >> 1;
if (value(Mid, i) >= value(Mid, pos))
R = (t = Mid) - 1;
else
L = Mid + 1;
}
return L;
}
inline void update(void) {
h = 1; t = 0;
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++) {
++q[h].l;
if (h <= t && q[h].r < q[h].l) ++h;
if (h > t || value(n, i) > value(n, q[t].pos)) {
while (h <= t && value(q[t].l, q[t].pos) < value(q[t].l, i)) --t;
if (h > t) q[++t] = tri(i, i, n);
else {
int pos = find(i, q[t].pos, q[t].l, q[t].r);
q[t].r = pos - 1;
q[++t] = tri(i, pos, n);
}
}
f[i] = value(i, q[h].pos);
}
for (int i = 1; i <= n; i++)
ans[i] = max(ans[i], (int)ceil(f[i]));
}
int main(void) {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
read(n);
for (int i = 1; i <= n; i++) read(a[i]);
update();
reverse(a + 1, a + n + 1);
reverse(ans + 1, ans + n + 1);
update();
reverse(ans + 1, ans + n + 1);
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);
return 0;
}