题目传送门


考虑如何去掉这个绝对值,

换句话说,如何减少边数并且能建出 MST。

在求解偏序问题时,往往会分而治之,

这样原来 \(O(n^2)\) 的做法就能够被优化。

考虑将所有点对半折开,

左半部分和右半部分的建边分治处理,

只考虑横跨两部分的建边,

若左半部分的点为 \(j\),右半部分的点为 \(i\)

边权就可以转换成:

\[(A_i+i\times d)+(A_j-j\times d)
\]

既然 \(i\) 和 \(j\) 独立出来,那么只需要选出

右半部分 \(\min\{A_i+i\times d\}\) 所对应的 \(i\),

左半部分 \(\min\{A_j-j\times d\}\) 所对应的 \(j\),

将 \(j\) 与右半部分所有点连边,\(i\) 同理。

那么这样就只有 \(O(n\log n)\) 条边,

用 Kruskal 跑最小生成树就可以做到 \(O(n\log^2n)\)。


Code

#include <cstdio>
#include <cctype>
#include <algorithm>
#define rr register
using namespace std;
const int N=200011; typedef long long lll;
struct node{int x,y; lll w;}e[N<<5];
lll a[N],b[N],D,ans; int n,f[N],m;
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
bool cmp(node x,node y){return x.w<y.w;}
inline signed getf(int u){return f[u]==u?u:f[u]=getf(f[u]);}
inline void dfs(int l,int r){
if (l==r) return;
rr int mid=(l+r)>>1,t0=l,t1=r;
for (rr int i=l;i<=mid;++i)
if (a[t0]>a[i]) t0=i;
for (rr int i=r;i>mid;--i)
if (b[t1]>b[i]) t1=i;
for (rr int i=l;i<=mid;++i)
e[++m]=(node){i,t1,a[i]+b[t1]};
for (rr int i=r;i>mid;--i)
e[++m]=(node){t0,i,a[t0]+b[i]};
dfs(l,mid),dfs(mid+1,r);
}
signed main(){
n=iut(),D=iut();
for (rr int i=1;i<=n;++i){
rr int x=iut(); f[i]=i;
a[i]=x-i*D,b[i]=x+i*D;
}
dfs(1,n),sort(e+1,e+1+m,cmp);
for (rr int i=1;i<=m;++i){
rr int fa=getf(e[i].x),fb=getf(e[i].y);
if (fa!=fb) f[fa]=fb,ans+=e[i].w;
}
return !printf("%lld",ans);
}

#Kruskal,分治#AT4569 Connecting Cities的更多相关文章

  1. 【LeetCode】1135. Connecting Cities With Minimum Cost 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Kruskal算法 日期 题目地址:https://l ...

  2. AtCoder keyence2019 E Connecting Cities

    keyence2019_e $N$ 个节点的无向图 $G$,节点 $i,j$ 之间的边权值为 $|i - j| \times D + A_i + A_j$ . 求最小生成树(Minimum Spann ...

  3. LeetCode 1135. Connecting Cities With Minimum Cost

    原题链接在这里:https://leetcode.com/problems/connecting-cities-with-minimum-cost/ 题目: There are N cities nu ...

  4. KEYENCE Programming Contest 2019 Solution

    A - Beginning 签到. #include <bits/stdc++.h> using namespace std; int main() { ]; while (scanf(& ...

  5. HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...

  6. hdu4126_hdu4756_求最小生成树的最佳替换边_Kruskal and Prim

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:  Portal: hdu4126 hdu4756  原题目 ...

  7. hdu4126Genghis Khan the Conqueror (最小生成树+树形dp)

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others) Total Submiss ...

  8. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  9. csuoj 1116: Kingdoms

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 1116: Kingdoms Time Limit: 3 Sec  Memory Limit ...

  10. CodeForces Round 192 Div2

    This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...

随机推荐

  1. 万字Java进阶笔记总结

    JavaApi 字符串 String 注意:Java中"=="操作符的作用: 基本数据类型:比较的是内容. 引用数据类型比较的是对象的内存地址. StringBuffer/Stri ...

  2. 用Taro写一个微信小程序——引入外部字体

    小程序直接用.ttf字体文件,在开发工具看没有问题,但是打包编译会提示这个字体文件打包上传不成功. 一.字体文件转换为css 1.打开https://transfonter.org/ 注意选择Base ...

  3. RabbitMQ零碎整理,总有一天在你工作中会用到

    概念 MQ:消息队列(消息中间件),开源的基于AMQP协议的消息中间件,异步 解耦 削峰 核心作用:1.异步 2.解耦 3.消息通信 RabbitMQ 消息的类型:1.点对点 2.Worker 3.发 ...

  4. 【Azure 存储服务】调用REST API获取Stroage Account Table中所有的Entity计数 -- Count

    问题描述 在Storage Account的使用中,如果想获取Table中全部Entity的计数以及大小,如果是REST API方式,如何来获取呢? 问题解答 在Azure中,所有服务的Metrics ...

  5. Java 一悟结束异常处理 Biu丶

  6. Java interface 接口 新特性

    1 package com.bytezreo.interfacetest; 2 3 public class SubClass2 { 4 public static void main(String[ ...

  7. form 表单提交 保存的时候再提交文件,之前一直是选择文件就传了,这个也比较好

    form 表单提交 保存的时候再提交文件,之前一直是选择文件就传了,这个也比较好 代码 <Upload :action="action" :max-size="ma ...

  8. Java GUI swing 可执行程序 jar (java -jar)idea

    创建项目 可以用maven idea设置 into 那个 参考资料 Java 基础 https://www.liaoxuefeng.com/wiki/1252599548343744/12604545 ...

  9. http-server -S 开启 https 服务

    下载 openssl Win64 OpenSSL v1.1.1k Light http://slproweb.com/download/Win64OpenSSL_Light-1_1_1k.exe 一路 ...

  10. stm32f103 实现LCD显示及分析

    前记   stm32 f103 在很多物联网系统中,状态显示是一个很重要的部分,在配合其它的系统做物联网设备时候,有一个状态显示,不仅仅是显得高大上,并且能够让你的系统变得更加人性化,这对于做设备的来 ...