题解 P2605 【[ZJOI2010]基站选址】

· · 题解

感觉没有黑题的难度吧.......

我们首先考虑一个朴素的 DP,一个很显然的想法就是让 dp_{i,j} 表示前 i 个位置,放了 j 个基站,补偿仅考虑下标 [1,i] 中的点的最小花费。

这样我们再从 i' 转移到 i 的时候,再统计下标在 [i'+1,i-1] 中的点的补偿就可以了,不难列出转移方程:

dp_{i,j}=\min_{1\le i'<i}\{dp_{i',j-1}+\operatorname{cost}(i'+1)\}+c_i

其中 \operatorname{cost}(i'+1) 表示在下标在 [i'+1,i-1] 中的点需要支付的补偿和。

如果我们直接在转移的时候暴力 O(n) 求解 \operatorname{cost} 的话,总的复杂度是 O(n^2k) 的,无法通过。

我们不难发现转移的瓶颈是在求解 \operatorname{cost} 上的,我们不妨将其和 dp_{i',j-1} 一起看待,再采用某种方法的在转移的过程中动态维护最小值。

我们不妨令最左边(dis 值最小的)能够覆盖到 i 点的基站位置为 L_i,同理最右边(dis 值最大的)能够覆盖到点 i 的基站位置为 R_i。那么如果我们在下标在 [L_i,R_i] 中的任意一个点放置了基站,点 i 将被覆盖,不会产生补偿。

我们考虑一个 L_i,R_i,在什么时候他会对 dp_{i',j-1}+\operatorname{cost}(i'+1) 的值产生贡献呢?一定是当 [L_i,R_i] 这个区间被 [i'+1,i-1] 这个区间包含的时候会产生贡献。再进一步,当对 [R_i+1,n] 中的 i 进行转移时,会对 [1,L_i-1] 中的 i' 产生影响。

由于我们是顺序枚举 i,所以我们只需要在 i=R_i 完成转移以后,将 [1,L_i-1] 中所有 i' 的 \operatorname{cost} 加上 W_i 即可,相当于我们动态维护了 \operatorname{cost} 的值。

我们需要区间加、区间最小值,线段树即可。

需要注意的是最后答案并不是 dp_{n,k},而应该是 \min_{1\le i\le n}dp_{i,k}+\operatorname{cost}(i)。

总的复杂度 O(nk\log n)。

code(需要 c++11):


// Problem: P2605 [ZJOI2010]基站选址
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2605
// Memory Limit: 125 MB
// Time Limit: 1000 ms ~ 5000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//By luogu @longer_name

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define fi first
#define se second
#define rg register
#define mp make_pair
#define pb push_back
#define pf push_front

#define rep(i, s, t) for(rg int i = s; i <= t; i++)
#define per(i, s, t) for(rg int i = t; i >= s; i--)
#define debug(x) cerr << (#x) << " = " << (x) << "\n"
#define OK cerr << "OK!\n"


namespace fastio {
    const int SIZE = (1 << 20) + 1;
    char ibuf[SIZE], *iS, *iT, obuf[SIZE],*oS = obuf, *oT = obuf + SIZE - 1;
    char _st[55];
    int _qr = 0;

    inline char getchar() {
        return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++) : *iS++);
    }
    inline void qread() {}
    template <typename T1, typename... T2>
    inline void qread(T1 &x, T2 &...ls) {
        x = 0;
        rg char ch = ' ';
        rg int ps = 1;
        while (!isdigit(ch) && ch != '-') ch = getchar();
        if (ch == '-') ps = -1, ch = getchar();
        while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
        x *= ps;
        qread(ls...);
    }

    inline void flush() {
        fwrite(obuf, 1, oS - obuf, stdout);
        oS = obuf;
        return ;
    }
    inline void putchar(char _x) {
        *oS++ = _x;
        if(oS == oT) flush();
    }
    template <typename T>
    inline void qwrite(T x) {
        if (x < 0) putchar('-'), x = -x;
        if (x < 10) return putchar('0' + x), void();
        qwrite(x / 10), putchar('0' + (x % 10));
    }

    template <typename T>
    inline void qwrite(T x, char ch) {
        qwrite(x), putchar(ch);
    }
}; //namespace fastio

ll qpow(ll x, ll p, ll mod) {
    rg ll base = x, res = 1;
    while(p) {
        if(p & 1) res = (res * base) % mod;
        base = (base * base) % mod;
        p >>= 1;
    }
    return res;
}

const int N = 2e4 + 5;
const int K = 105;
const ll inf = 1e18;

ll dp[N];
ll mi[N << 2], lazy[N << 2];
ll c[N], s[N], dis[N], w[N];
int n, k;
vector<pair<int, int> > st[N];

void Lazy(int cur, int x) {
    mi[cur] += x;
    lazy[cur] += x;
}

void PushDown(int cur) {
    Lazy(cur << 1, lazy[cur]);
    Lazy(cur << 1 | 1, lazy[cur]);
    lazy[cur] = 0;
}

void PushUp(int cur) {
    mi[cur] = min(mi[cur << 1], mi[cur << 1 | 1]);
}

void Build(int cur, int l, int r) {
    lazy[cur] = 0;
    if(l == r) return mi[cur] = dp[l], void();
    rg int mid = l + r >> 1;
    Build(cur << 1, l, mid);
    Build(cur << 1 | 1, mid + 1, r);
    PushUp(cur);
}

ll Query(int cur, int ql, int qr, int l, int r) {
    if(ql <= l && r <= qr) return mi[cur];
    PushDown(cur);
    rg int mid = l + r >> 1;
    rg ll res = inf;
    if(ql <= mid) res = min(res, Query(cur << 1, ql, qr, l, mid));
    if(mid < qr) res = min(res, Query(cur << 1 | 1, ql, qr, mid + 1, r));
    return res;
}

void Modify(int cur, int ql, int qr, int l, int r, ll x) {
    if(ql <= l && r <= qr) return Lazy(cur, x), void();
    PushDown(cur);
    rg int mid = l + r >> 1;
    if(ql <= mid) Modify(cur << 1, ql, qr, l, mid, x);
    if(mid < qr) Modify(cur << 1 | 1, ql, qr, mid + 1, r, x);
    PushUp(cur);
}

int main() {
    fastio::qread(n, k);
    rep(i, 2, n) fastio::qread(dis[i]);
    dis[0] = -inf, dis[n+1] = inf; //给 lower_bound 和 upper_bound 的边界赋值,避免越界
    rep(i, 1, n) fastio::qread(c[i]);
    rep(i, 1, n) fastio::qread(s[i]);
    rep(i, 1, n) fastio::qread(w[i]);
    rep(i, 1, n) {
        int L = lower_bound(dis, dis + n + 1, dis[i] - s[i]) - dis;
        int R = upper_bound(dis, dis + n + 1, dis[i] + s[i]) - dis - 1;
        st[R].pb(mp(L, i)); //将 Ri 挂在 vector 上
    }
    ll res = 0, ans = inf;
    rep(i, 1, n) {
        dp[i] = res + c[i];
        for(auto x : st[i]) res += w[x.se];
    }
    ans = min(ans, dp[n]);
    rep(j, 1, k) {
        Build(1, 0, n);
        rep(i, 1, n) {
            dp[i] = Query(1, 0, i-1, 0, n) + c[i];
            for(auto x : st[i]) Modify(1, 0, x.fi - 1, 0, n, w[x.se]);
        }
        ans = min(ans, Query(1, 0, n, 0, n));
    }
    fastio::qwrite(ans, '\n');
    fastio::flush();
    return 0;
}