题解:AT_keyence2019_e Connecting Cities

· · 题解

思路

拆解代价式子(假设 i<j):

原式化为:

w=(j-i)\times D+A_i+A_j

开括号得:

w=(A_i-D\times i)+(A_j+D\times j)

用 X_i 和 Y_i 代替两式,得:

w=X_i+Y_j

还有一个重要的结论需要大家了解。

MST 中的两条边,不会交叉选择,即对于 a<b<c<d,不会同时选择 (a,c) 与 (b,d)。

这个其实可以感性理解。如果要总代价最小,那么肯定是选择相邻的点更优。

于是就可以开始做题了。

  1. 在 MST 构建的任意时刻,每个连通块对应的点一定是一段连续的区间(套结论);

  2. 对于一段连续的区间 [l,r],左侧边的端点应在 [1,l-1] 中取 X 最小,[l,r] 中取 Y 最小。或者右侧边 [l,r] 取 X 最小,[r+1,n] 取 Y 最小。两种方案取最小值。

看到区间最小值,使用线段树维护(虽然不用修改)。

题面明显告诉我们用最小生成树,这里推荐用 Boruvka。连边的时候用上述方法无脑写就行。

线段树查询 \mathcal{O(\log n)},Boruvka 外层循环 \mathcal{O(\log n)},总复杂度 \mathcal{O(n\log^2 n)},在 n\le 2\times 10^5 时可以接受。

:::success[代码]

#include <bits/stdc++.h>
#define int long long
#define ac int mid=l+r>>1,ls=p<<1,rs=p<<1|1
#define akioi int ls=p<<1,rs=p<<1|1
using namespace std;
const int N=2e5+10,inf=1e18;
int n,d;
int ans;
int a[N];
int x[N],y[N];
int f[N];
int bl[N],br[N];
inline int find(const int& x){
    if(f[x]==x) return x;
    return f[x]=find(f[x]);
}
struct seg{
    struct node{
        int v,pos;
    } s[N*4];
    void pushup(int p){
        akioi;
        if(s[ls].v<s[rs].v)
            s[p]=s[ls];
        else
            s[p]=s[rs];
        return;
    }
    void build(int p,int l,int r,const int a[]){
        if(l==r){
            s[p]={a[l],l};
            return;
        }
        ac;
        build(ls,l,mid,a);
        build(rs,mid+1,r,a);
        pushup(p);
    }
    node query(int p,int l,int r,int L,int R){
        if(L>r||l>R) return {inf,-1};
        if(L<=l&&r<=R) return s[p];
        ac;
        node tx=query(ls,l,mid,L,R);
        node ty=query(rs,mid+1,r,L,R);
        return tx.v<ty.v? tx:ty;
    }
};
seg mx,my;
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>d;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        x[i]=a[i]-d*i;
        y[i]=a[i]+d*i;
    }
    mx.build(1,1,n,x);
    my.build(1,1,n,y);
    for(int i=1;i<=n;++i){
        f[i]=i;
        bl[i]=br[i]=i;
    }
    while(1){
        vector<int> rt;
        for(int i=1;i<=n;++i)
            if(find(i)==i)
                rt.push_back(i);
        if(rt.size()==1) break;
        vector<tuple<int,int,int>> edge;
        for(auto root:rt){
            int l=bl[root],r=br[root];
            int bestc=1e18;
            int bestu=-1,bestv=-1;
            if(l>1){
                auto tx=mx.query(1,1,n,1,l-1);
                auto ty=my.query(1,1,n,l,r);
                int cost=tx.v+ty.v;
                if(cost<bestc){
                    bestc=cost;
                    bestu=tx.pos;
                    bestv=ty.pos;
                }
            }
            if(r<n){
                auto tx=mx.query(1,1,n,l,r);
                auto ty=my.query(1,1,n,r+1,n);
                int cost=tx.v+ty.v;
                if(cost<bestc){
                    bestc=cost;
                    bestu=tx.pos;
                    bestv=ty.pos;
                }
            }
            if(bestu!=-1)
                edge.emplace_back(bestc,bestu,bestv);
        }
        for(auto &e:edge){
            int w=get<0>(e);
            int u=get<1>(e);
            int v=get<2>(e);
            int fx=find(u);
            int fy=find(v);
            if(fx!=fy){
                ans+=w;
                if(fx>fy) swap(fx,fy);
                f[fy]=fx;
                bl[fx]=min(bl[fx],bl[fy]);
                br[fx]=max(br[fx],br[fy]);
            }
        }
    }
    cout<<ans;
    return 0;
}

:::