码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 2216 [Poi2011]Lightning Conductor 决策单调性+dp

时间:2018-08-14 22:50:05      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:否则   ==   geo   影响   .com   cpp   时间   sqrt   经典   

题面

题目传送门

解法

决策单调性比较经典的题吧

题目就是要对于每一个\(i\)\(f_i=max(a_j-a_i+\sqrt{|i-j|}))\)

可以发现,\(\sqrt n\)的增长速度比较慢,所以满足决策单调性

决策单调性是指,如果决策\(j\)对于\(i\)已经不是最优的了,那么在后面也一定不是最优的

我们可以对于每一个\(i\)记录它是由哪一个决策\(j\)转移而来的

可以发现,只要出现在决策表中的数一定构成若干段区间

那么,我们只要开一个队列,记录每一个决策的转移区间即可

假设当前队尾为决策\(p\),对应最优区间为\([l,r]\)

如果在\(l\)这个位置发现\(i\)\(p\)优,那么直接把\(p\)删掉

如果在\(r\)这个位置发现\(i\)没有\(p\)优,那么就可以不用管了

否则,二分中间的断点,更新区间

用一个双端队列来实现这个过程

注意:本题需要考虑上取整等操作,所以在转移的时候不要把double转成int,否则会影响结果

时间复杂度:\(O(n\ log\ n)\)

代码

#include <bits/stdc++.h>
#define N 500010
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == ‘-‘) f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - ‘0‘, c = getchar(); x *= f;
}
struct Node {
    int p, l, r;
} q[N];
int a[N], f[N], g[N];
double calc(int x, int y) {
    return (double)a[y] + sqrt(abs(y - x));
}
int solve(int pos, int L, int R, int i) {
    int l = L, r = R, ans = -1;
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (calc(mid, pos) <= calc(mid, i)) r = mid - 1, ans = mid;
            else l = mid + 1;
    }
    return ans;
}
int main() {
    int n; read(n);
    for (int i = 1; i <= n; i++) read(a[i]);
    int h = 1, t = 0;
    for (int i = 1; i <= n; i++) {
        q[h].l++;
        while (h <= t && q[h].r < q[h].l) h++;
        if (h > t || calc(n, q[t].p) < calc(n, i)) {
            while (h <= t && calc(q[t].l, q[t].p) < calc(q[t].l, i)) t--;
            if (h <= t) {
                int x = solve(q[t].p, q[t].l, q[t].r, i);
                q[t].r = x - 1;
                q[++t] = (Node) {i, x, n};
            } else q[++t] = (Node) {i, i, n};
        }
        f[i] = ceil(calc(i, q[h].p) - a[i]);
    }
    reverse(a + 1, a + n + 1);
    memset(q, 0, sizeof(q));
    h = 1, t = 0;
    for (int i = 1; i <= n; i++) {
        q[h].l++;
        while (h <= t && q[h].r < q[h].l) h++;
        if (h > t || calc(n, q[t].p) < calc(n, i)) {
            while (h <= t && calc(q[t].l, q[t].p) < calc(q[t].l, i)) t--;
            if (h <= t) {
                int x = solve(q[t].p, q[t].l, q[t].r, i);
                q[t].r = x - 1;
                q[++t] = (Node) {i, x, n};
            } else q[++t] = (Node) {i, i, n};
        }
        g[i] = ceil(calc(i, q[h].p) - a[i]);
    }
    for (int i = 1; i <= n; i++) cout << max(0, max(f[i], g[n - i + 1])) << "\n";
    return 0;
}

bzoj 2216 [Poi2011]Lightning Conductor 决策单调性+dp

标签:否则   ==   geo   影响   .com   cpp   时间   sqrt   经典   

原文地址:https://www.cnblogs.com/copperoxide/p/9478281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!