[单调队列][决策单调性DP] BZOJ 2216: [Poi2011]Lightning Conductor

Solution S o l u t i o n

考虑 ji j ≤ i j>i j > i

ansi=max{aj+|ij|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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值