1D1D决策单调性dp

int a[200]; int n;
struct node { 
    int l, r, p;
    //node() {}
    //node(int a, int b, int c) :l(a), r(b), p(c) {}
}q[2000];

double f[200], g[200];

double cal(int i, int j) {
    return a[j] + sqrt(i - j) - a[i];
}

int find(node q, int x) {
    int l = q.l, r = q.r;
    while (l <= r) {
        int m = l + r >> 1;
        if (cal(m, q.p) > cal(m, x)) l = m + 1;
        else r = m - 1;
    }
    return l;
}

void Dp(double *f) {
    int head = 1, tail = 0;
    for (int i = 1; i <= n; ++i) {
        q[head].l++;
        if (head <= tail && q[head].l > q[head].r) ++head;
        if (head > tail || cal(n, q[tail].p) < cal(n, i)) {
            while (head <= tail && cal(q[tail].l, q[tail].p) < cal(q[tail].l, i)) --tail;
            if (head > tail) q[++tail] = node{ i, n, i };
            else {
                int t = find(q[tail], i);
                q[tail].r = t - 1;
                q[++tail] = node{ t, n, i };
            }
        }
        f[i] = cal(i, q[head].p);
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    Dp(f);
    for (int i = 1; i <= n / 2; ++i) swap(a[i], a[n - i + 1]);
    Dp(g);
    for (int i = 1; i <= n; ++i) {
        printf("%d\n", max(0, (int)ceil(max(f[i], g[n - i + 1]))));
    }
    return 0;
}

 

posted @ 2017-03-28 11:15  BOSON+CODE  阅读(273)  评论(0编辑  收藏  举报