[Problem] Luogu p2605 [zjoi2010] Base Station Site Selection

Source: Internet
Author: User
Original question Portal: p2604 [zjoi2010] site selection for the Base Station, change to know that this question must be due to the DP setting of F [I] [J], indicating that the J-th base station is built in the village I without considering I + 1 ~ The minimum cost of N villages can be obtained from F [I] [J] = min (F [k] [J-1] + cost [k] [I]). + C [I] (j-1 <= k <I) where cost [k] [I] indicates I ~ There is no fee required for base stations between K for computing complexity O (N), plus loops, total complexity O (N ^ 2 k). Let's take a look at the data range k <= N, k <= 100, n <= 20000 awesome. TLE considers how to optimize it. First, we find that the previous transfer equation can remove one-dimensional J, in fact, as long as J is enumerated in the outermost layer, F [I] = min (F [k] + cost [k] [I]) can be obtained. + C [I] (j-1 <= k <I) The main time is wasted on computing cost. We need to find a method to optimize for any village I, record the left and right boundary st [I] And Ed [I] that can be covered. (The leftmost and rightmost ends can cover the position of the I base station. binary search can be used for processing) then, when the neighboring table is used to record the villages whose ed value is I, the base stations created before these villages will not be able to cover I. In this way, when we derive I + 1 ~ If St [k]-1 (Ed [k] = I) is transferred, the village KK fee must be compensated, we can use the line segment tree to maintain the value of F [k] + cost [k] [I] in the range [1, st [k]-1] with the village K fee, the transfer is in the range [1, i-1] Find the minimum value of F [k] + cost [k] [I]. The total complexity is O (n log n k ).
#pragma GCC optimize("O3")#include <bits/stdc++.h>#define N 20005#define inf 0x3f3f3f3fusing namespace std;inline int read(){    register int x=0,f=1;register char ch=getchar();    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}    return x*f;}inline int Min(register int a,register int b){    return a<b?a:b;}int D[N],C[N],S[N],W[N],lr[N],rr[N],f[N];vector<int> vq[N];int ql,qr;struct Segment_tree{    int minn[N<<2],tag[N<<2];    inline void pushup(register int x)    {        minn[x]=Min(minn[x<<1],minn[x<<1|1]);    }    inline void build(register int x,register int l,register int r)    {        tag[x]=0;        if(l==r)        {            minn[x]=f[l];            return;        }        int mid=(l+r)>>1;        build(x<<1,l,mid);        build(x<<1|1,mid+1,r);        pushup(x);    }    inline void maintain(register int x,register int l,register int r)    {        if(l!=r)            pushup(x);        minn[x]+=tag[x];    }    inline void update(register int x,register int l,register int r,register int v)    {        if(ql<=l&&qr>=r)            tag[x]+=v;        else        {             int mid=(l+r)>>1;            if(ql<=mid)                update(x<<1,l,mid,v);            if(qr>mid)                update(x<<1|1,mid+1,r,v);        }         maintain(x,l,r);    }    inline int query(register int x,register int l,register int r,register int v)    {        if(ql<=l&&qr>=r)            return minn[x]+v;        int ret=inf,mid=(l+r)>>1;        if(ql<=mid)            ret=Min(ret,query(x<<1,l,mid,v+tag[x]));        if(qr>mid)            ret=Min(ret,query(x<<1|1,mid+1,r,v+tag[x]));        return ret;    }}T;int main(){    int n=read(),k=read();    for(register int i=2;i<=n;++i)        D[i]=read();    for(register int i=1;i<=n;++i)        C[i]=read();    for(register int i=1;i<=n;++i)        S[i]=read();    for(register int i=1;i<=n;++i)        W[i]=read();    ++n,++k;    D[n]=inf;    for(register int i=1;i<=n;++i)    {        lr[i]=lower_bound(D+1,D+n+1,D[i]-S[i])-D;        rr[i]=lower_bound(D+1,D+n+1,D[i]+S[i])-D;        if(D[i]+S[i]<D[rr[i]])            --rr[i];        vq[rr[i]].push_back(i);    }    int ans=inf;    for(register int j=1;j<=k;++j)    {        if(j==1)        {            int tot=0;            for(register int i=1;i<=n;++i)            {                f[i]=tot+C[i];                for(register int tmp=0;tmp<vq[i].size();++tmp)                    tot+=W[vq[i][tmp]];            }            ans=Min(ans,f[n]);            continue;        }        T.build(1,1,n);        for(register int i=1;i<=n;++i)        {            ql=1,qr=i-1;            int add=qr?T.query(1,1,n,0):0;            f[i]=add+C[i];            for(register int tmp=0;tmp<vq[i].size();++tmp)            {                ql=1,qr=lr[vq[i][tmp]]-1;                if(qr>0)                    T.update(1,1,n,W[vq[i][tmp]]);            }        }        ans=Min(ans,f[n]);    }    printf("%d",ans);    return 0;       }

[Problem] Luogu p2605 [zjoi2010] Base Station Site Selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.